C++ C++;程序要求我输入两次,即使变量第一次被赋值
每次我试图输入我的答案,它需要我输入两次。即使ExamCore1第一次已经被分配了一个值,它仍然需要我输入另一个值。我设计了这个程序,如果输入的不是数字,它将输出一条错误消息,并要求用户再次输入一次,然后程序将完全结束C++ C++;程序要求我输入两次,即使变量第一次被赋值,c++,cin,C++,Cin,每次我试图输入我的答案,它需要我输入两次。即使ExamCore1第一次已经被分配了一个值,它仍然需要我输入另一个值。我设计了这个程序,如果输入的不是数字,它将输出一条错误消息,并要求用户再次输入一次,然后程序将完全结束 cout << "Please enter score for Exam 1: "; cin >> examScore1; if(!(cin >> examScore1)||!(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;
}
}
cout>examcore1;
如果(!(cin>>ExamCore1)| |!(ExamCore1=0))
{
cout>examcore1)| |!(examcore1=0)
{
您的问题是您多次请求cin。
首先,你要确定你的价值观;
没问题。
但是,在if中,您可以编写if(!(cin>>ExamCore1))
这本质上是要求计算机再次从控制台读取值。正确的方法是如果(!examcore1)
另外,您想用if(!(cin>>examcore1))实现什么?从您的代码中,我猜您希望用户输入的分数从0到100,如果用户输入的是无效的,您的程序只允许再输入一次,如果用户再次输入残疾人,则退出。关于您的问题,关于为什么您的代码要求更多的输入,我将尝试在这里简要解释
cout << "Please enter score for Exam 1: ";
cin >> examScore1;
我想你想检查一下,但请稍等。在第一句话中
!(cin >> examScore1)
你需要更多的输入,这就是为什么你的代码在你输入分数后仍然要求输入。如果你只想检查它,就这样做吧
if(examScore1 > 100 || examScore1 < 0){
//Tell the user that the score is wrong
}
if(examcore1>100 | | examcore1<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;
}
cout>examcore1)| |!(examcore1=0)
{
cout 100 | | examcore1<0){
cout examcore1;
如果(ExamCore1>100 | | ExamCore1<0){
cout>examcore1;
如果(ExamCore1>100 | | ExamCore1<0){
你可以有cin>>examcore1;
和下一行if(!(cin>>examcore1)
,所以你要问两次谢谢。我之所以使用(!(cin>>examcore1)是因为我想处理字母和数字。你可以为此抛出异常。我使用if(!(cin>>examcore1)以防万一,如果输入了一个字母或任何不适合int变量的东西。啊,好吧。这不会给你你想要的结果。这是一个类还是什么的。因为我可以给你一个快速函数,可以做你想要的。
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";
}
}