I have written a program that allows the user to insert scores and the program then provides the average score, the number of scores entered, the amount of each letter grade, and the highest and lowest score. I've also used a sentinal to exit. I'm new to C++ and this is only my second program. I understand it is probably redundant and I've done a lot of extra things I could have done easier but it works and it's me. The only part I'm having trouble with is listing the highest and lowest score. I'm lost! Any help would be appreciated. Here is my code:

#include<iostream>
using namespace std;
int main()
{
    cout.setf(ios::fixed)
    ;
    cout.setf(ios::showpoint);
    cout.precision(2);
    
    double score = 0;
    char letterGrade;
    const double A_GRADE = 89.5;
    const double B_GRADE = 79.5;
    const double C_GRADE = 69.5;
    const double D_GRADE = 59.5;
    const double F_GRADE = 49.5;
    int examNumber = 0;
    int aCount = 0;
    int bCount = 0;
    int cCount = 0;
    int dCount = 0;
    int fCount = 0;
    double gpaTotal = 0;
    const int A_GPA = 4;
    const int B_GPA = 3;
    const int C_GPA = 2;
    const int D_GPA = 1;
    const int F_GPA = 0;
    
    
    
    while (score >= 0)
    {
        cout << "Please enter an exam score (negative to summarize) --> ";
        cin >> score;
        if (score > 100)
        
        {
            cout << "Please enter valid score (0-100) --> ";
            cin >> score;
        }
        
        
        if( score >=A_GRADE && score <= 100 )
        {
            letterGrade = 'A';
            aCount++;
            examNumber++;
            gpaTotal += A_GPA;
        }
        else
        {
            if( score >=B_GRADE && score < A_GRADE )
            {
                letterGrade = 'B';
                bCount ++;
                examNumber++;
                gpaTotal += B_GPA;
            }
            else
            {
                if( score >=C_GRADE && score < B_GRADE )
                {
                    letterGrade = 'C';
                    cCount ++;
                    examNumber++;
                    gpaTotal += C_GPA;
                }
                else
                {
                    if( score >=D_GRADE && score < C_GRADE )
                    {
                        letterGrade = 'D';
                        dCount ++;
                        examNumber++;
                        gpaTotal += D_GPA;
                    }
                    else
                    {
                        if (score >= 0)
                        {
                            letterGrade = 'F';
                            fCount ++;
                            examNumber++;
                            gpaTotal += F_GPA;
                        }
                        
                    }
                }
            }
        }
        
    }
    cout << "The total number of exams entered: " << examNumber << endl;
    cout << "The average GPA is: " << gpaTotal/examNumber << endl;
    cout << "Number of A's: " << aCount << endl;
    cout << "Number of B's: " << bCount << endl;
    cout << "Number of C's: " << cCount << endl;
    cout << "Number of D's: " << dCount << endl;
    cout << "Number of F's: " << fCount << endl;
    
    return 0;
}

Hello there.

You'll probably want to change those else { if() { statements to simply else if() - much shorter, and it makes your code look neater. Speaking of which, please use code tags. :) (more info on my signature)

There's several ways you can calculate the highest and the lowest score. One way is to store all the scores in an array, and then sort them out at the end. Another way is to keep 2 variables, highestScore and lowestScore. Each time a grade is entered, check it against these 2. If it's higher than the highest score, update highestScore accordingly. You get the idea.

You'll probably find the latter easier, however if I were writing it, I'd do the former, more because it's a shorter and more flexible method. Your choice.

I will remember to use those tags next time. I am going to try your suggestions now and let you know how they work out for me. Thank you so much!

Ok...I figured it out using your second suggestion. I haven't learned arrays yet. I also cleaned up the else ifs. It works perfectly, although it looks messy to me. Are there any shortcuts I could have taken? I really want to learn!

#include <iostream>
using namespace std;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
 
double score = 0;
char letterGrade;
const double A_GRADE = 89.5;
const double B_GRADE = 79.5;
const double C_GRADE = 69.5;
const double D_GRADE = 59.5;
const double F_GRADE = 49.5;
int examNumber = 0;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
double gpaTotal = 0;
const int A_GPA = 4;
const int B_GPA = 3;
const int C_GPA = 2;
const int D_GPA = 1;
const int F_GPA = 0;
int highScore = 0;
int lowScore = 100;
double examTotal = 0;
while (score >= 0)
{
cout << "Please enter an exam score (negative to summarize) --> ";
cin >> score;
if (score > highScore && score <= 100 ) highScore = score;
if (score < lowScore && score >= 0 ) lowScore = score;

if (score > 100) 

{ cout << "Please enter valid score (0-100) --> ";
cin >> score;
}


if( score >=A_GRADE && score <= 100 )
{ 
letterGrade = 'A';
aCount++;
examNumber++;
gpaTotal += A_GPA;
examTotal += score;
 
}
else
{
if( score >=B_GRADE && score < A_GRADE )
{ 
letterGrade = 'B';
bCount ++;
examNumber++;
gpaTotal += B_GPA;
examTotal += score;
}
else 
{
if( score >=C_GRADE && score < B_GRADE )
{
letterGrade = 'C';
cCount ++;
examNumber++;
gpaTotal += C_GPA;
examTotal += score;
}
else 
{
if( score >=D_GRADE && score < C_GRADE ) 
{
letterGrade = 'D';
dCount ++;
examNumber++;
gpaTotal += D_GPA;
examTotal += score;
}
else 
{
if (score >= 0)
{
letterGrade = 'F';
fCount ++;
examNumber++;
gpaTotal += F_GPA;
examTotal += score;
}

}
}
}
}

}
cout << "The total number of scores entered: " << examNumber << endl;
cout << "The average score is: " << examTotal/examNumber << endl;
cout << "The highest score is: " << highScore << endl;
cout << "The lowest score is: " << lowScore << endl;
cout << "Number of A's: " << aCount << endl;
cout << "Number of B's: " << bCount << endl;
cout << "Number of C's: " << cCount << endl;
cout << "Number of D's: " << dCount << endl;
cout << "Number of F's: " << fCount << endl;
cout << "The average GPA is: " << gpaTotal/examNumber << endl;

return 0;
}

You still haven't fixed the else ifs.

else
{
if( score >=B_GRADE && score < A_GRADE )
{ 
letterGrade = 'B';
bCount ++;
examNumber++;
gpaTotal += B_GPA;
examTotal += score;
}

It should look like this:

else if( score >=B_GRADE && score < A_GRADE )
{ 
letterGrade = 'B';
bCount ++;
examNumber++;
gpaTotal += B_GPA;
examTotal += score;
}
else if ...

Proper indentation is also good to practice; most editors do it automatically for you, so look for an option to enable it if it isn't already!

The main thing I can think of for improving your program is to use arrays; however since you haven't learned them yet I'm not going to bug you about them. ;) Once you learn them, you'll be able to make your code look a little neater.

...although it looks messy to me.

It sure is :)

I really want to learn!

Good. Then take Joe's advice and learn formatting.

After every { indent 4 spaces. Before every } unindent 4 spaces. That would make this section look like:

if( score >=A_GRADE && score <= 100 )
{ 
    letterGrade = 'A';
    aCount++;
    examNumber++;
    gpaTotal += A_GPA;
    examTotal += score;
 
}
else
{
    if( score >=B_GRADE && score < A_GRADE )
    { 
        letterGrade = 'B';
        bCount ++;
        examNumber++;
        gpaTotal += B_GPA;
        examTotal += score;
    }
    else 
    {
        if( score >=C_GRADE && score < B_GRADE )
        {
            letterGrade = 'C';
            cCount ++;
            examNumber++;
            gpaTotal += C_GPA;
            examTotal += score;
        }
        else 
        {
            if( score >=D_GRADE && score < C_GRADE ) 
            {
                letterGrade = 'D';
                dCount ++;
                examNumber++;
                gpaTotal += D_GPA;
                examTotal += score;
            }
            else 
            {
                if (score >= 0)
                {
                    letterGrade = 'F';
                    fCount ++;
                    examNumber++;
                    gpaTotal += F_GPA;
                    examTotal += score;
                }
            }
        }
    }
}

I like the way you added spaces in each line. That's a very good thing. It really helps readability :mrgreen:

As for the code, when you get to if( score >=B_GRADE && score < A_GRADE ) , haven't you already taken account of the score < A_GRADE part of the if because of the previous if?
Couldn't you make this one simply if( score >=B_GRADE )

Are there any shortcuts I could have taken? I really want to learn!

Some points:
• Instead of representing GPA's with seperate const variables which clutter the code, you could have used [search]enum[/search] or enumerations which group the GPA's logically in a single unit. Something like:

enum GPA { F, D, C, B, A } ; // here F = 0, E = 1 and so on
// OR something like
enum GPA { A = 4, B = 3, C = 2, D = 1, F = 0 } ;

• Instead of representing grades and count using separate variables, you can use enumerations for representing the indices and make your task easier. For eg.

enum GRADES { A_grade, B_grade, C_grade, D_grade, F_grade } ; 
 // A_grade = 0 and so on

const double grade_arr[] = { 89.5, 79.5, 69.5, 59.5, 49.5 } ;

// now access the grades with something like grade_arr[ A_grade ]

enum COUNT { A_count, B_count, C_count, D_count, F_count } ;

int count_arr[ SZ ] ; // here SZ = 5, the number of grades

// now make changes to the count array as
count_arr[ A_count ] = 9 ;

• Don't forward declare variables until you plan on using them, it will save you from a lot of obscure bugs and hard to maintain code. In short declare the variables in the minimum scope you want them to persist and if possible just before the point of their use.

Hope you liked my post, I am trying to be nice you know....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.