hi! i need a little help with this assignment from school. i have most of the parts done but i have a small error and i have no idea how to fix it. can someone please help me? my output looks almost the same but it displays a different average and letter grade then its supposed to. my output is all the way down. (below the sample output)

so this is what the output is supposed to look like:

How many students do you wish to process? 3
Enter the name of student number 1 --> Bob Jones
Enter a grade for Bob Jones or (-999) to exit --> 90
Enter a grade for Bob Jones or (-999) to exit --> 100
Enter a grade for Bob Jones or (-999) to exit --> 70
Enter a grade for Bob Jones or (-999) to exit --> 70
Enter a grade for Bob Jones or (-999) to exit --> -999

Bob Jones has an average of 83 and made a B

Enter the name of student number 2 --> Arnold Palmer
Enter a grade for Arnold Palmer or (-999) to exit --> 100
Enter a grade for Arnold Palmer or (-999) to exit --> 92
Enter a grade for Arnold Palmer or (-999) to exit --> -999

Arnold Palmer has an average of 96 and made an A

Enter the name of student number 3 --> Jack Nicklaus
Enter a grade for Jack Nicklaus or (-999) to exit --> 98
Enter a grade for Jack Nicklaus or (-999) to exit --> 92
Enter a grade for Jack Nicklaus or (-999) to exit --> 93
Enter a grade for Jack Nicklaus or (-999) to exit --> -999

Jack Nicklaus has an average of 94 and made an A

my program:

#include <iostream>
#include <stdio.h>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
int n = 1;
int Students = 0;
int Average = 0;
string StudentNameFirst;
string StudentNameLast;

cout << "How many students do you wish to process? ";
cin >> Students;
cout << endl;
cout << "Enter the name of student" << n << " --> ";
cin >> StudentNameFirst >> StudentNameLast;
cout << endl;

for (int k = 0; k < Students; k++)
{
n++;
{
cout << "Enter a grade for " << StudentNameFirst << " " << StudentNameLast << " or (-999) to exit: ";
{
cout << endl;

}

}

cout << endl;

cout << StudentNameFirst << " " << StudentNameLast << " has an average of " << Average << " and made a " << LetGrade << endl;
}

}
Last edited on
You did not provide us with your output.

What does your program do incorrectly?

Last edited on
You are prompting for the name of the student outside the loop, so you only prompt for one student name.

Right now your code assumes that a name is exactly two words. I'd store the entire name in a single string and use getline() to read it. That way the name could be "Dave" or "David Hayden" or "David M. Hayden" or "Mr. David M. Hayden MS."

I urge you to add some debugging code to print out the name after you read it. After you read the last grade for one student, you'll have to consume the newline before reading the name, and printing the name out will help you debug this.

You'll need to set GradeNumCount and Average to zero at the start of the loop. Otherwise they will contain the values from the previous iteration. This is one reason why it's good to declare variables just before you need them: if you move the declaration to inside the loop then they will be initialized to zero each time.

You're computing the average grade using ints. That means the result will be truncated to an integer. You should use double instead. When you print it, do cout << setprecision(2) to cause it to print 2 significant digits only.
okay so I tried it and i got it to work the way i want it. thanks doug4 and dhayden!!
Topic archived. No new replies allowed.