-1

I need to use -1 to terminate but still display the summary. Every time that I've tried and have gotten it to terminate the program, it doesn't go ahead and display the summary. There are up to 10 trials, if you don't have enough information for 10 and you want to stop at 8, you type -1 and it goes to the summary and then terminates the program

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");
sashkello
  • 17,306
  • 24
  • 81
  • 109
confuseduser
  • 23
  • 1
  • 1
  • 4
  • 3
    Just debug your program. Use print statements or gdb to find out what happens when -1 is entered. No need to post on SO for this :p – keyser Nov 10 '13 at 10:04
  • 1
    @confuseduser Please don't edit your question with "thank you" post. SO is supposed to be a repository of useful questions and answers. When your problem is solved, let others to use this knowledge. Thank people with upvotes and accept the most useful answer. – sashkello Nov 10 '13 at 22:23

2 Answers2

0

You don't want two loops, only one. You need to combine your two conditions into one i<10 && test.

Also you have declared your test variable in the wrong places. You should declare it once at the beginning of your loop.

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";
    }
}
john
  • 85,011
  • 4
  • 57
  • 81
0

Try to use break; when -1 is inputted inside the while loop. Also, you can use 1 loop, instead of two as john mentioned above.

Another thing to look for is your last for loop, it goes from 0 to 9, but in case someone used -1 and only inputted 3 grades, there might be odd values for the solutions.

user2699298
  • 1,446
  • 3
  • 17
  • 33