Looping Back to Beginning

Hello, I am working on a project which I would like to loop back to the beginning of the program upon bad cin. The code is not complete but around line 30 is where I would like to have the program loop back to the beginning. The program output will look roughly like this when it loops (cin in bold):

Would you like a car wash today? yes

Which wash would you like: Super, Premium, or Ultra? super

That will be $5.00, is that okay? no way!

I'm sorry...


Would you like a car wash today? YES!!!

Which wash would you like: Super, Premium, or Ultra? Ultra

That will be $8.00, is that okay? Yes.

Thank you!
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
#include <iostream>
#include <climits>
#include <cctype>

using namespace std;

int main(void)
{
    char choice1,choice2,choice3;
	const double super = 5.00,
                premium = 6.00,
                ultra = 8.00;
    cout << "Would you like a car wash today?\n";
    cin >> choice1;  
    cin.ignore(INT_MAX, '\n');
    choice1 = toupper(choice1);
	while ( (choice1) != 'Y' )
		{
		cout << "\n\nHave a wonderful day!!\n\n" << endl;
		return 0;
    }
	cout << "Which wash would you like: Super, Premium, or Ultra?  ";
			cin >> choice2;
			cin.ignore(INT_MAX, '\n');
			choice2 = toupper(choice2);
			while (choice2 != 'S' && choice2 != 'P' && choice2 != 'U' )
{		
	cout << "\n\aInvalid choice!!\n\n"
                    "Try again!!!\n\n";
		
}			
			if ( 'S' == choice2 )
				cout << "That will be $" << super << ", is that okay?\n";
			else if ( 'P' == choice2 )
				cout << "That will be $" << premium << ", is that okay?\n";
			else if ( 'U' == choice2 )
				cout << "That will be $" << ultra << ", is that okay?\n";
				
  cout << "\n\nHave a wonderful day!!" << endl;
    return 0;
}

Last edited on
If you want it to loop, put the whole thing in a while loop
To check for bad input with a while loop, your statement should look somewhat like this:

while(!(cin >> choice2))
{
cout << "Bad input!" << endl;
cin.clear();
cin >> BadIn; //variable where you'd send bad input
cout << "Please try again: " << endl;
}

and you can place this wherever necessary to avoid having to return to the beginning every time an error is made

or you can also put lines 22 - 30 in a do while loop with the same argument, play around with it

by the way, there really isn't a need for choice2, since you're only using it to store a character response, just like what choice1 does, and since it's not necessary for choice1 to retain it's 'y' or 'n' response anyway.
I have changed the code to the following but it still doesn't loop back when the conditions aren't met in the while loop at the bottom. Any ideas? Thanks!
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
int main(void)
{
    char choice1,choice2;
	const double super = 5.00,
                premium = 6.00,
                ultra = 8.00;
do
{
    cout << "Would you like a car wash today?\n";
    cin >> choice1;  
    cin.ignore(INT_MAX, '\n');
    choice1 = toupper(choice1);
if (choice1 != 'Y')
{
		cout << "\n\nHave a wonderful day!!\n\n" << endl;
		return 0;
	}	
else 
{
cout << "Which wash would you like: Super, Premium, or Ultra?  ";
			cin >> choice2;
			cin.ignore(INT_MAX, '\n');
			choice2 = toupper(choice2);
}
if ( 'U' == choice2 )
{
	cout << "That will be $" << ultra << ", is that okay?\n";
}		
else if ( 'S' == choice2 )
{
	cout << "That will be $" << super << ", is that okay?\n";
}
else if ( 'P' == choice2 )
{
	cout << "That will be $" << premium << ", is that okay?\n";
}
else
{	
cout << "\n\aInvalid choice!!\n\n"
	"Try again!!!\n\n";
}
	}
while (choice1 == 'Y' && choice2 == 'S' && choice2 == 'P' && choice2 == 'U');
				
cout << "\n\nHave a wonderful day!!" << endl;
return 0;
}
in line 43 you're saying that it should loop back to the beginning only if choice2 is to equal S, P, and U all at the same time.
Last edited on
Topic archived. No new replies allowed.