Question 1: Build a number guesing game that uses input validation (isdigit() fu
ID: 3545691 • Letter: Q
Question
Question 1: Build a number guesing game that uses input validation (isdigit() function) to verify that the user has entered a digit and not a non-digit (letter). Store a random number between 1 and 10 into a variable each time the program is run. Prompt the user to guess a number between 1 and 10 and alert the user if he was correct or not.
Question 2: Build a Fortune Cookie program that uses either the Chinese Zodiac or astrological signs to generate a fortune, a prediction, or a horoscope based on the user
Explanation / Answer
int answer = int(rand() % 10 + 1);//Randomly put a number from 1 to 10
int num = 0;
cout << ("Enter a number.");
cin >> num;
if(isdigit(num))//If the number is some number from 0 to 9
{
if(num == answer){ cout <<"That is right!";}
else{cout << "That is wrong!" ;}
}
else{ cout <<"That isn't a number!";}
The problem with isdigit() is that it is: 0,1,2,3,4,5,6,7,8,9, but not 10. If the guess happens to be 10 it will tell you that 10 isn't a number. Otherwise if it was from 0 to 9 it would not be a problem. You may have to manipulate the input and/or output.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.