Looking for answers

Hello C++ world, I am need of your help. What I need is search terms because I am obviously looking in the wrong places because I can't get my answer. I have a program that takes in numeric input and I want to give the user an error if they input a string. So what am I looking for? I have searched online for C++ errors for hours and found maybe only 5 examples but, when I plug them into my program it is a no go. Can you please help me? Thanks in advance.
Last edited on
http://www.cplusplus.com/forum/articles/6046/

Read this one, it may help you
Thank you vichu8888 for your response. I got it working just fine now but, I have my program set up so after it's done running it asks the user if they would like to run it again using a while loop. When I pres 'y' for yes it gives me my error how do I prevent this. This is the part of the code I used:

1
2
3
4
5
6
7
8
9
10
while (true) {
   cout << "Please enter a valid number: ";
   getline(cin, input);

   // This code converts from string to number safely.
   stringstream myStream(input);
   if (myStream >> myNumber)
     break;
   cout << "Invalid number, please try again" << endl;
 }


Thank you for your time.
You have clear the stream before the program execute second time i.e) after user enters 'y' it should be cleared.

So insert cin.ignore(); right after you get the response from the user 'y' or 'n'
something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char response;
do
	{
		
		while (true) 
		{
			cout << "Please enter a valid number: ";
			getline(cin, input);

			// This code converts from string to number safely.
			stringstream myStream(input);
			if (myStream >> myNumber)
				break;
			cout << "Invalid number, please try again" << endl;
		}

		
		cout<<"Do you check for another(y/n): ";
		cin>>response;
		response=toupper(response);
		cin.ignore();
	}while(response=='Y');
vichu8888 you are the man or woman, cin.ignore(); worked perfectly thank you very much. I will now mark this as solved. Again thank you for your time.
Last edited on
You are welcome and Im a man ;)
Topic archived. No new replies allowed.