Need help with looping

Hello, I am totally new to C++. I'm looking to repeat this program with a simple y/n. But I also want it set up to only accept y or n as proper inputs. Right now, anything other than 'y' will bring up the "invalid entry" text, but the program loops all the same. I can get rid of "processMenu" altogether and just use a do/while loop, which was working fine, but again, anything other than 'y' would exit the program, and I want to make y and n the only two possible answers. Can anyone help? I know this code I'm posting here doesn't work, in fact, it might not even be compiling at the moment, so I apologize, it's about 5 in the morning here and I'm due some sleep. Thanks in advance for any help.

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
  int processMenu()
{
	char choice;
	string mystr;
	float price = 0;
	int quantity = 0;

	do //loop until user is done
	{
		cout << "Enter price: ";
		getline(cin, mystr);
		stringstream(mystr) >> price;
		cout << "Enter quantity: ";
		getline(cin, mystr);
		stringstream(mystr) >> quantity;
		cout << "Total price: " << price*quantity << endl;
		cout << "Calculate more prices? y/n \n" << endl;
		cin >> choice;
		getchar();
	} while (choice == 'y');
	return 0;
}

int main()
{
	cout << "Welcome! \n";

	while (!processMenu())
	{
		cout << "Sorry, that is not a valid choice." << endl;
		cout << "Please, try again." << endl << endl;
	}
	return 0;
}
why two loop is required?
I mean in main while loop is not required if u want to exit the program on press of no 'n' there only if is enough here.
Well that's what I'm asking. How would I get the program to only respond to y or n? With the code as above, it loops as it should when I press y, when I press any other key it moves to the error displays, but yet it just continues looping the program anyway instead of repeating the option to press y or n. I guess I might be explaining it badly, too. Like I said, early in the morning here, and little sleep.
In main
change as following
if (!processMenu())
cout << "Sorry, that is not a valid choice." << endl;
cout << "Please, try again." << endl << endl;
}

It will exit the program .
That's what u want or something else?
Last edited on
Hmmm, when I changed it as you suggested, n just simply acts like y and loops the program back around. Any other input closes the program still. Like I could input q, and the program will close. And the error messages don't pop up at all now, like "sorry, not a valid choice", isn't displaying no matter what I try.
Basically what I want the program to do is perform the math, ask if I want to do it again, if I hit y, it starts again, if I hit n, it exits. If I hit anything other than y or n, it tells me that's an invalid input and to try it again.
something like this

label :
do //loop until user is done
{
cout << "Enter price: ";
getline(cin, mystr);
//std::stringstream ss >> mystr >> price;
cout << "Enter quantity: ";
getline(cin, mystr);
//std::stringstream sa >> mystr >> quantity;
cout << "Total price: " << price*quantity << endl;
cout << "Calculate more prices? y/n \n" << endl;
cin >> choice;
getchar();
if(choice == 'n' || choice =='N')
exit;
else
{
cout << "Sorry, that is not a valid choice." << endl;
cout << "Please, try again." << endl << endl;

goto label;
}
} while (choice == 'y');
return 0;
}

int main()
{
cout << "Welcome! \n";
processMenu();


return 0;
}

Thanks for the help, man, I finally got it. It's not exactly what you typed out for me, but it gave me the starting point that led to the fix. Everything loops and exits perfectly now. Again, thanks so much.

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
void func()
{
	char choice;
	string mystr;
	float price = 0;
	int quantity = 0;

	{
		cout << "Enter price: ";
		getline(cin, mystr);
		stringstream(mystr) >> price;
		cout << "Enter quantity: ";
		getline(cin, mystr);
		stringstream(mystr) >> quantity;
		cout << "Total price: " << price*quantity << endl;
LOOP:
		cout << "Calculate more prices? y/n \n" << endl;
		cin >> choice;
		getchar();
		if (choice == 'n' || choice == 'N')
			exit;
		else if (choice == 'y' || choice == 'Y')
			func();
		else
		{
			cout << "Invalid response, please try again." << endl;
			goto LOOP;
		}
	} 
}

int main()
{
	cout << "Welcome! \n\n";

	func();

		return 0;
}
Topic archived. No new replies allowed.