-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 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");
4

2 回答 2

0

你不想要两个循环,只有一个。您需要您的两个条件合二为一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 result\n";
        i++;
    }
    ...
    else
    {
        test=false;
        cout << "Invalid Input!\n";
    }
}
于 2013-11-10T10:05:20.470 回答
0

尝试break;在 while 循环中输入 -1 时使用。此外,您可以使用 1 个循环,而不是上面提到的 john 的两个。

要寻找的另一件事是您的最后一个 for 循环,它从 0 到 9,但如果有人使用 -1 并且只输入 3 个等级,则解决方案可能会有奇数值。

于 2013-11-10T11:46:02.713 回答