How to get program to recognize if user enters anything but a number

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int x;
string favword;

int main()
{
cout << "Enter your favorite number (between 1 and 10 ... don't mess around ima computer.... I'll know" <<endl;

do
{
cin >> x ;
if (x < 1)
{
cout << "Not cool" <<endl;
}

else if (x > 10)
{
cout << "Man I can't count that high" <<endl;

}

else if (x = string)
{
cout << "SYSTEM ERROR"<<endl;
exit(0);
}

}while(x < 1 || x > 10);



cout << "Well done" <<endl;
cout << "Now enter you favorite word" <<endl;
cin >> favword;
cout <<"this is your favorite word " << favword <<endl;
system("pause");
return 0;
}








I wan't this program to be able to recognize if a user types in any word ex. (apple, tree, house etc..) and print "system error" if a word is recognized not to be a number. The bold section of code is my failed attempt to try this. Basically I need to know what to make

(x = ?(every word)) in order to finish my console program. thanks for the 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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "Enter an integer between 1 and 10" << endl;
    int x;
    
    do
    {
        while (!(cin >> x))
        {
            cout << "not an integer" << endl;
            cin.clear();           // reset error flags
            cin.ignore(100, '\n'); // ignore the unwanted input    
        }
        
        if (x < 1)
        {
            cout << "Not cool" << endl;
        }
        else if (x > 10)
        {
            cout << "Man I can't count that high" << endl;
        
        }
    } while (x < 1 || x > 10);
    
    system("pause");
    return 0;
}
Last edited on
while (!(cin >> x))
So does this mean when there is an error with the input of x, or does in mean not equal to?
Thanks for the Help!
This part attempts to get some input cin >> x which may or may not be an integer.

After that, the true or false status of the expression is tested. If a valid integer was input, cin will evaluate as true. Conversely if the input was not an integer (the operation failed), cin will evaluate to false.

The ! reverses the condition, so that the body of the while loop will execute when cin gives false, in other words when the input was not an integer.

Thanks that actually makes sense! Seeing as you seem to know what your talking about I was wondering if you could help me with this code.

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>


using namespace std;

int main()
{
	srand((unsigned)time(0));
	int MyNumber = rand()%(100);
	int guess;
	int counter = 0;
	
	
	cout <<"Guess my number 1-100"<<endl;
	do
	{
	
	
	
		
		cin >> guess;
		counter++;
		if(guess < MyNumber)
		{
			cout << "Too Small Guess Again"<<endl;
		}

		if(guess > MyNumber)
		{
			cout << "Too Big Guess again"<<endl;
			
		}


		system("pause");
		system("cls");
	}while(guess!= MyNumber);
	
	cout<<"Correct YOU WIN it took you "<<counter<<" guesses"<<endl;
	system("pause");
	return 0;
}


I got the program to make a guessing game where I guess a number and the program gives me directions to narrow my guess, but how do you make the console guess a number that the user have in mind. (ex. I'm thinking of the number 12, the console guesses a random number lets say 40. If I tell the console that it's guess is too high, how do you make the console take another guess using the input from the user so the next guess is closer to the actual number... and so on. If you know how to fix this problem could you also explain the code involved? Thank you so much for helping me I'm just learning c++ and this is really helping!
The key to solving this problem is to set an upper and lower limit.
Let's say the number can be in the range 1 to 100.
1
2
int lowerLimit = 1;
int upperLimit = 100;


Then get the computer to make a guess.
1
2
int guess;
guess = lowerLimit  + rand() % (upperLimit - lowerLimit +1);


After each user input, you change either the upper or lower limit.

Then the computer makes another guess and so on.
The upper and lower limits will get closer and closer until they are equal, at which point there is no need to guess, the number must be the same as the limits.
Thankyou!! I understand the concept but it is still a bit confusing Ill try to experiment and see what I get.
Topic archived. No new replies allowed.