uszy123345
uszy123345

Reputation: 43

is there a simpler way to write this portion of the code?

        if (grade < 0 || grade > 100)
            {
                outputFile << right << setw(2) << number << "." << setw(5) << grade << "  INVALID" << endl;
                ++number;
                invalid++;
            }
            else
            {
                outputFile << right << setw(2) << number << "." << setw(5) << grade << endl;
                ++number;
                total += grade;
                valid++;
            }

//I'm new to this so basically is there a way to use a simplify this loop? basically I don't want to display the outputFile twice since the only difference is the invalid word at the end if the number is below 0 or over a 100

Upvotes: 1

Views: 45

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37192

Here's one way:

outputFile << right << setw(2) << number << "." << setw(5) << grade;
if (grade < 0 || grade > 100)
{
    outputFile << "  INVALID";
    invalid++;
}
else
{
    total += grade;
    valid++;
}
outputFile << endl;
++number;

Upvotes: 3

Related Questions