#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace 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;
}
elseif (x > 10)
{
cout << "Man I can't count that high" <<endl;
}
elseif (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
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.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
usingnamespace 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!
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.