LOOP Help

Oct 24, 2016 at 3:20am
i am trying to have a way to make the program goback to the top of the while loop. When the user enters 'No' for their answer, i want to have the loop go back to where it says "what kind of wash".

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
  #include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;



int main() {

	char s;
	char choice;
	char answer;

	cout << "want wash " << endl;
	cin >> s;
	while (s == 'y'){
		cout << "What kind of wash" << endl;
		cout << "s,p,u" << endl;
		cin >> choice;
		cin.ignore(INT_MAX, '\n');
		if (toupper(choice) == 'P') {
			cout << "is that okay" << endl;
			cin >> answer;
		}
	}
	
	
	


	system("pause");
	return 0;
}

Oct 24, 2016 at 9:02am
Currently because s doesn't change you have either an infinite loop or it doesn't loop at all. So once you see "What kind of wash" you will always see it again no matter what.
Oct 24, 2016 at 12:43pm
Should I have an if statement for s and ten then the while loop within that statement
Oct 24, 2016 at 1:12pm
Hello fivestar,
If you want to put an if statement in before the while loop to check the value of "s" you can, but it is not necessary because if "s" is not equal to "y" the while loop will fail and not execute. I would suggest using tolower or toupper to catch either case of the letter entered. just like you did on line 22.

At line 22 you could either use if statements or a switch to deal with entry for choice and I would add a choice to quit the while loop. I would more likely use a switch myself.

Give it some thought and a try and see what happens.

Hope that helps,

Andy
Last edited on Oct 24, 2016 at 1:13pm
Topic archived. No new replies allowed.