简体   繁体   中英

Need help do-while loop program? (C++)

I am trying to create a program that will take user inputs(marks) and output the average of these marks to a letter grade. I believe I have succeded in getting the program to calculate the average succesfully, however it will not output the right letter grade?

Any suggestions? EDIT: Perhaps it is something in the calculations, because my output always gives the percentage as an "F" grade....

// Used iomanip to display averages for possible three digit or greater answers.
#include <iostream>
#include <iomanip>
using namespace std;

// Set up my program using only main()
int main()
{
    //Created the variable 'mark' so the user input can be stored and tested in the do-while condition.
    //Variable 'sum' and 'average' to calculate mark and then be tested against letter grades.
    //'counter' used to keep track of number of terms being averaged.
    double mark=0, sum=0, average=0;
    int counter=-1;

    // Do-while statement to test the loop. Gets input from user, and tests whether it is a a valid letter grade.
    // Marks below 0 or above 100 will test true and loop back re-promting the user for another mark.
    // If tested condition is false, then if statements then test the mark and output the letter grade.

    cout << "Please enter marks to calculate average, and enter -1 to stop and find letter equivalence. " << endl;

    do
    {
        if ( mark < 0 || mark > 100 )
        {
            cout << "Entered mark is invalid. Please try again, or enter -1 to stop and find letter equivalence. " << endl;
            break;
        }

        sum = sum + mark;
        counter++;
        cin >> mark;

    }
    while ( mark != -1 );

    average = sum / counter ;

    //What happens when above statement is false...
    if (mark >= 90)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ ";
        }
    else if (mark >=80)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A ";
        }
    else if (mark >=70)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of B ";
        }
    else if (mark >=60)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of C ";
        }
    else if (mark >=50)
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of D ";
        }
    else
        {
            cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of F ";
        }
return 0;
}

Couple of things to consider:

  1. Your grades assignment should consider the average and not marks . If you use marks, then the last entered value which is -1 to exit your do while loop will be used to grade which will always result in F grade.

  2. Add endl to all your cout statements after the do-while loop. The output stream may have been buffered and this can cause the cout statements to not appear on your monitor. endl will flush the output stream.

For eg.
cout << "The average of "<<setprecision(2)<<average<<"% is equivalent to a letter grade of A+ " << endl;

Double-check your if statements. I'd wager that you're always seeing the correct average is equivalent to an F.

At the end of your loop, mark is always -1 . So all the tests fail, you fall through to the else clause, and output an F. The fix is to test average , not mark .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM