C++程序要求我输入两次,即使变量是第一次赋值
C++ Program requires me to input twice even though the variable gets assigned the first time
每次我尝试输入我的答案时,它都要求我输入两次。即使 examScore1 已经在第一次赋值,它仍然需要我输入另一个值。我设计了这个程序,如果输入的不是数字,它会输出一条错误消息,并要求用户重新输入一次,然后程序就会完全结束。
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "Exam score cannot be less than 0 or more than 100" << endl;
cout << "Please re-enter score for Exam 1: \n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "\nInvalid input entered. PROGRAM WILL END. Please\n"
<< "consult the user manual and restart the program\n";
validInput = false;
}
}
你的问题是你多次请求 cin。
首先你 cin 你的价值 cin >> examScore1;
没问题。
但是在你的 if 中,你写 if(!(cin >> examScore1))
这实际上是要求计算机再次从控制台读取值。正确的做法是 if(!examScore1),
另外,你想用 if(!(cin >> examScore1)) 达到什么目的?
根据你的代码,我猜想你希望用户输入 score
范围从 0 到 100,如果用户输入无效,你的程序只允许再输入一次,然后在用户输入时退出再次成为残疾人。关于为什么您的代码需要更多输入的问题,我将在此处简要解释一下。
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
此处您的代码要求输入,然后将其放入 examScore1
。之后
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
我想你想检查一下,但请稍等。在第一个语句中
!(cin >> examScore1)
您需要更多输入,这就是为什么您的代码在您输入乐谱后仍继续要求输入的原因。如果你只想检查它,就这样做
if(examScore1 > 100 || examScore1 < 0){
//Tell the user that the score is wrong
}
在那之后,你想再给用户一次输入的机会。在您的代码中
cout << "Exam score cannot be less than 0 or more than 100" << endl;
cout << "Please re-enter score for Exam 1: \n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "\nInvalid input entered. PROGRAM WILL END. Please\n"
<< "consult the user manual and restart the program\n";
validInput = false;
}
哇老兄,冷静点:)。你只是想让他们输入另一个。只是很好地问了他们。
cout << "Please input score in range (0-100)";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Wow dude, read the doc please";
}
最后结束,你的代码看起来更像这个
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Please input score in range (0-100)";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Wow dude, read the doc please";
}
}
每次我尝试输入我的答案时,它都要求我输入两次。即使 examScore1 已经在第一次赋值,它仍然需要我输入另一个值。我设计了这个程序,如果输入的不是数字,它会输出一条错误消息,并要求用户重新输入一次,然后程序就会完全结束。
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "Exam score cannot be less than 0 or more than 100" << endl;
cout << "Please re-enter score for Exam 1: \n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "\nInvalid input entered. PROGRAM WILL END. Please\n"
<< "consult the user manual and restart the program\n";
validInput = false;
}
}
你的问题是你多次请求 cin。 首先你 cin 你的价值 cin >> examScore1; 没问题。 但是在你的 if 中,你写 if(!(cin >> examScore1)) 这实际上是要求计算机再次从控制台读取值。正确的做法是 if(!examScore1),
另外,你想用 if(!(cin >> examScore1)) 达到什么目的?
根据你的代码,我猜想你希望用户输入 score
范围从 0 到 100,如果用户输入无效,你的程序只允许再输入一次,然后在用户输入时退出再次成为残疾人。关于为什么您的代码需要更多输入的问题,我将在此处简要解释一下。
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
此处您的代码要求输入,然后将其放入 examScore1
。之后
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
我想你想检查一下,但请稍等。在第一个语句中
!(cin >> examScore1)
您需要更多输入,这就是为什么您的代码在您输入乐谱后仍继续要求输入的原因。如果你只想检查它,就这样做
if(examScore1 > 100 || examScore1 < 0){
//Tell the user that the score is wrong
}
在那之后,你想再给用户一次输入的机会。在您的代码中
cout << "Exam score cannot be less than 0 or more than 100" << endl;
cout << "Please re-enter score for Exam 1: \n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> examScore1;
if(!(cin >> examScore1)||!(examScore1 <= 100 || examScore1 >= 0))
{
cout << "\nInvalid input entered. PROGRAM WILL END. Please\n"
<< "consult the user manual and restart the program\n";
validInput = false;
}
哇老兄,冷静点:)。你只是想让他们输入另一个。只是很好地问了他们。
cout << "Please input score in range (0-100)";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Wow dude, read the doc please";
}
最后结束,你的代码看起来更像这个
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Please input score in range (0-100)";
cin >> examScore1;
if(examScore1 > 100 || examScore1 < 0){
cout << "Wow dude, read the doc please";
}
}