The following program I wrote prompts the user to input a number 1 - 100. The program has a pre-selected number from a random number generator that the user has to guess. If the user's guess is too high or low then the program will notify the user until the user's guess is right. My program works well, but when prompted to input a number I have to input the number four or more times in some case for the program to prompt me. Could someone show me or help me to find my mistake?
Thank you
#include <iostream>
#include <ctime>//A user stated that using this piece of code would make a true randomization process.
#include <cstdlib>
using namespace std;
int main()//When inputing a number, sometimes the user has to input it more than once.
{
int x;
int ranNum;
srand( time(0));
ranNum = rand() % 100 + 1;
cout << "Please input your guess for a random number between 1 - 100.\n";
cin >> x;
while (x > ranNum || x < ranNum)
{
{
if (x > ranNum)
cout << "Your input was greater than the system's generated random number. Please try again.\n";
cin >> x;
if (x > 100 || x < 1)
cout << "Input invalid. Please input a number between 1 - 100.\n";
cin >> x;
}
{
if (x < ranNum)
cout << "Your input was less than the system's generated random number. Please try again.\n";
cin >> x;
if (x > 100 || x < 1)
cout << "Input invalid. Please input a number between 1 - 100.\n";
cin >> x;
}
{
if (x == ranNum)
cout << "You guessed right!\n";
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire