Help with "while" loop structure.

Hello all, I am very new to C++ programming so please bear with me. Basically, the problem I am have with the code below is that I want any input other that 'w', 'W', 'h', 'H', 'O', 'o', and 'f', 'F' to return an invalid entry and start the loop over so that the user has an opportunity to make a valid input. Instead, currently any input besides the ones specified previously terminate the program. Feel free to berate me, but any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*******************************************************************************************************
 Name: David Cole
 Date: 1/30/2014
 *******************************************************************************************************/

#include <iostream>
#include <iomanip>

using namespace std;

//declare constants
const double hawaiiCost = 425.00;
const double westernCost = 318.00;
const double otherCost = 375.50;
const double luggageCost = 16.00;
const double discount = 0.25;

int main()
{
	//Declare variables
	char destination, payment;
	int numberOfTickets;
	int numberOfYoungChildren;
	int numberOfBags;
	double totalCost = 0;
	double subTotal;
    
	cout << fixed << showpoint << setprecision(2);
	
	cout << "Welcome to David's Airfare calculator" << endl << endl;
	
	//Begin loop structure
    
    while (destination != 'F' || destination != 'f')
    {
		numberOfTickets = 0;
		numberOfYoungChildren = 0;
		subTotal = 0;
        
		//Ask user for input
		cout << "What is your destination: W)estern state, H)awaii, O)ther, F)inished?  ";
		cin >> destination;
		cout << endl;
        
		if (destination == 'W' || destination == 'w' || destination == 'H' || destination == 'h' || destination == 'O' || destination == 'o')
		{
            
			cout << "How many tickets would you like? ";
			cin >> numberOfTickets;
			cout << endl;
            
			cout << "How many passengers are 3 and younger? ";
			cin >> numberOfYoungChildren;
			cout << endl;
            
			switch (destination)
			{
                case 'w':
                case 'W':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * westernCost;
                    break;
                case 'h':
                case 'H':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * hawaiiCost;
                    break;
                case 'o':
                case 'O':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * otherCost;
                    break;
                default:
                    subTotal = 0;
			}
			totalCost = totalCost + subTotal;
		}
        
		else if (destination != 'f' || destination != 'F')
		{
			break;
		}
        
		else
		{
			cout << "Invalid input, please try again. " << endl << endl;
		}
	}
    
    
	if (totalCost > 0)
	{
		//Ask user for further input
		cout << "Will you be using a Rickety Airlines Visa to purchase tickets (Y/N)? ";
		cin >> payment;
		cout << endl;
        
		cout << "How many bags do you have to check? ";
		cin >> numberOfBags;
		cout << endl;
        
		if (payment == 'Y' || payment == 'y')
		{
			totalCost = totalCost - (totalCost * discount);
		}
		
		cout << "Your total cost is $" << totalCost + (luggageCost * numberOfBags) << endl;
	}
	
	cout << "Thank you for using David's Airfare calculator. ";
	
	system("pause");
	
    
    
}
Last edited on
Alright, i think i understand what you mean. I switched some stuff around so let me know if this is what you want. to terminate it for f and F i just added another case statement and used an extra return 0; to end the program early.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
 
using namespace std;
 
int main()
{
 
        char destination;
        int westernCost,hawaiiCost,otherCost;
    	int numberOfTickets = 0;
	    int	numberOfYoungChildren = 0;
        int subTotal = 0;
        int totalCost = totalCost + subTotal;
 
 
		//Ask user for input
		cout << "What is your destination: W)estern state, H)awaii, O)ther, F)inished?  ";
		cin >> destination;
		cout << endl;
 
              switch (destination)
			{
 
                case 'w':
                case 'W':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * westernCost;
                    break;
                case 'h':
                case 'H':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * hawaiiCost;
                    break;
                case 'o':
                case 'O':
                    subTotal = (numberOfTickets - numberOfYoungChildren) * otherCost;
                    break;
		case 'f':
		case 'F':
		break;
                default:
                   cout << "Invalid Input, Please try again" <<endl;
				return 0;
                   break;
			}
 
 
 
			cout << "How many tickets would you like? ";
			cin >> numberOfTickets;
			cout << endl;
 
			cout << "How many passengers are 3 and younger? ";
			cin >> numberOfYoungChildren;
			cout << endl;
 
			
 
 
 
 
   return 0;
}

Last edited on
I don't want 'f' or 'F' to return invalid input. I want 'f' or 'F' to end the loop and move onto the next if structure. I want any other input besides W, H, O , F to return invalid input. I edited my post above to show the entire source code.

if you run my code youll notice f and F doesnt return invalid input. only default does. why would you put in the case for each lowercase letter then? delete those cases and simply have them included in the default.

I suggest looking at this:
http://mathbits.com/MathBits/CompSci/looping/end.htm
Last edited on
That's awesome. Thanks so much for the help.
Did that make sense? you had an extra loop that you really didnt need :P
Topic archived. No new replies allowed.