我编写的以下程序提示用户输入一个数字1 - 100.该程序有一个来自随机数发生器的预选号码,用户需要猜测。如果用户的猜测过高或过低,程序会通知用户,直到用户的猜测是正确的。我的程序运行良好,但是当提示输入一个数字时,我必须在某些情况下输入数字四次或更多次才能提示我。有人能让我看看或帮我找到我的错误吗?C++:提示需要多次输入数据才能继续
#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;
}
'while(x> ranNum || x