通过 -1 终止时出现问题
issue with terminating through -1
>我需要使用 -1 终止,但仍显示摘要。每次我尝试并让它终止程序时,它都不会继续显示摘要。最多有 10 个试验,如果您没有足够的信息来进行 10 次试验,并且想在 8 次时停止,则键入 -1 并转到摘要,然后终止程序
while(i<10) { do { cout << "Enter result """ << i+1 << """ (or -1 if no more results): "; cin >> score[i]; if(score[i] >=0 && score[i] <=49) { cout << "Grade " << "U" << " will be assigned to this resultn"; bool test=true; i++; } else if(score[i] >=50&& score[i] <=59) { cout << "Grade " << "P" << " will be assigned to this resultn"; bool test=true; i++; } else if(score[i] >=60 && score[i] <=69) { cout << "Grade " << "C" << " will be assigned to this resultn"; bool test=true; i++; } else if(score[i] >=70 && score[i] <=89) { cout << "Grade " << "B" << " will be assigned to this resultn"; bool test=true; i++; } else if(score[i] >=90 && score[i] <=100) { cout << "Grade " << "A" << " will be assigned to this resultn"; bool test=true; i++; }
else
{
test=false;
cout << "Invalid Input!n";
}
}
while(test);
}
cout << "nSummary of the results:n";
for(int a=0;a< 10;a++)
{
std::cout << std::fixed << std::setprecision(2) << "Result " << a+1 << " " << score[a] << " Grade " << determine_grade(score[a]) << "n";
}
cout << "nThe average of the results = " << calc_average(score) << "n";
cout << "The lowest of the results = " << find_lowest(score) << "n";
cout << "The highest of the results = " << find_highest(score) << "n";
system("Pause");
你不想要两个循环,只需要一个。您需要将两个条件合并为一个i<10 && test
。
此外,您在错误的位置声明了测试变量。您应该在循环开始时声明一次。
bool test = true;
while(i<10 && test)
{
cout << "Enter result """ << i+1 << """ (or -1 if no more results): ";
if(score[i] >=0 && score[i] <=49)
{
cout << "Grade " << "U" << " will be assigned to this resultn";
i++;
}
...
else
{
test=false;
cout << "Invalid Input!n";
}
}
当在
while 循环中输入 -1 时,尝试使用 break;
。此外,您可以使用 1 个循环,而不是上面提到的 john 的两个循环。
要寻找的另一件事是最后一个 for 循环,它从 0 到 9,但如果有人使用 -1 并且只输入了 3 个等级,则解决方案可能有奇数值。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在抛出 'std::invalid_argument' 的实例后终止调用 what(): stoi (找不到问题所在)
- C++程序问题:抛出'std::invalid_argument'实例后终止调用
- 可变参数模板中递归模板函数的终止问题
- (C++) 学习向量 - 编码/输入流终止/比较向量的问题
- 而循环不工作终止问题
- 而循环终止问题
- 这段代码出了什么问题?它在第二个 cin 可以执行之前终止
- CPP-Netlib 问题,在抛出 'boost::exception_detail::clone_impl 的实例后调用终止
- 通过 -1 终止时出现问题
- 这个程序有什么问题,它终止了?
- 和野兔模拟终止问题
- 程序在输入开尔文或摄氏后终止?(我的代码有什么问题?
- 如果应用程序存在内存泄漏,是否在终止时解决这些问题