C++ 通过-1终止的问题
我需要使用-1终止,但仍然显示摘要。 每次我试图让它终止程序时,它都不会继续显示摘要。 最多有10次试验,如果您没有足够的信息进行10次试验,并且您想在8次试验时停止试验,您可以键入-1,它将转到摘要,然后终止程序C++ 通过-1终止的问题,c++,termination,C++,Termination,我需要使用-1终止,但仍然显示摘要。 每次我试图让它终止程序时,它都不会继续显示摘要。 最多有10次试验,如果您没有足够的信息进行10次试验,并且您想在8次试验时停止试验,您可以键入-1,它将转到摘要,然后终止程序 while(i您不需要两个循环,只需要一个。您需要将两个条件组合成一个i当while循环中输入-1时,尝试使用break;。此外,您可以使用1个循环,而不是上面提到的两个循环 另一个需要查找的是您的最后一个for循环,它从0到9,但是如果有人使用-1,并且只输入了3个等级,那么解决方
while(i您不需要两个循环,只需要一个。您需要将两个条件组合成一个i当while循环中输入-1时,尝试使用break;
。此外,您可以使用1个循环,而不是上面提到的两个循环
另一个需要查找的是您的最后一个for循环,它从0到9,但是如果有人使用-1,并且只输入了3个等级,那么解决方案可能会有奇数值。只需调试您的程序。使用打印语句或gdb来了解输入-1时会发生什么。无需为此发布:p@confuseduser请不要编辑您的问题用“谢谢”的帖子表达感谢。SO应该是一个有用问题和答案的存储库。当你的问题得到解决时,让其他人利用这些知识。用投票感谢他人,并接受最有用的答案。
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 result\n";
bool test=true;
i++;
}
else if(score[i] >=50&& score[i] <=59)
{
cout << "Grade " << "P" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=60 && score[i] <=69)
{
cout << "Grade " << "C" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=70 && score[i] <=89)
{
cout << "Grade " << "B" << " will be assigned to this result\n";
bool test=true;
i++;
}
else if(score[i] >=90 && score[i] <=100)
{
cout << "Grade " << "A" << " will be assigned to this result\n";
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");
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 result\n";
i++;
}
...
else
{
test=false;
cout << "Invalid Input!\n";
}
}