1.11M Members

C++ Student Average Grade program

 
0
 

I'm trying to write a basic program for a C++ lab for class, and running into some trouble.
Senario is that the program averages grades. A user enters the number of grades, then enteres a letter grade, then the program runs for however many grades the user has, then averages them all together and outputs a GPA. Here's what i have so far:
Thanks in advance... and sorry if its bad code... i'm in a 100 level class, for IT security.. programming is not my strongpoint...

#include <iostream> 
using namespace std;
int main()

{
	char usergrade;
	int totalgrades = 0;
	int numgrade = 1;
	int count = 0;
	int average = 0;
	int A = 4, a = 4;
	int B = 3, b = 3;
	int C = 2, c = 2;
	int D = 1, d = 1;
	int F = 0, f = 0;

	cout << " Please enter the number of grades to average:\n";
	cin >> numgrade;

	while (count < numgrade)
		{
			cout << " Please enter grade in letter format:\n";
			cin >> usergrade;
			cout << " You entered " << usergrade << ".\n";
			totalgrades = usergrade + totalgrades;
			
			


			
			count++;
		}
	average = totalgrades / numgrade;

	cout << " Your GPA is: " << average << ".\n";
}

Hopefully you'll see my mistake.. I think what i need to do is find a way to assign the numerical value when the user enters the letter grade.

Any help would be awesome! Thanks in advance!

 
0
 

this one works for me:

cout<<"Enter number of grade input: ";
    cin>>numgrade;
    for (int x=0;x<numgrade;x++)
    {
        cout<<"Enter a grade in letter format: ";
        cin>>usergrade;
        switch (usergrade)
        {
               case 'A':
                    totalgrade+=4;
                    break;
                 //and so on...
 
0
 

When you post a request for help, you need to tell us what's wrong. Don't just tell us there's an error and expect us to look for the error.

 
0
 

This is simple to fix:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
     int A = 0, B = 0, C = 0, D = 0, F = 0;
     int grade, total;

     cout << "How many grades?: ";
     cin >> total;

     for (int count = 0; count == total; ++count)
     {
          cout << "1. A" << endl;
          cout << "2. B" << endl;
          cout << "3. C" << endl;
          cout << "4. D" << endl;
          cout << "5. F" << endl << endl;

          cout << "Select: ";
          cin >> grade;

          if (grade == 1)
               A++;

          if (grade == 2)
               B++;

          if (grade == 3)
               C++;

          if (grade == 4)
               D++;

          if (grade == 5)
               F++;
     }

     int average = ((A + B + C + D + F) / total);

     cout << "Average: " << average << endl << endl;

     system("PAUSE");
     return 0;
}

Boom. Problem solved. :)

 
0
 

Okay, I think i see where you are going with this, but there is one question I have.. Where does each grade letter get assigned the 0-4 value, or is the user doing that by entering the number?

I only ask, because I think the instructor is wanting the use to type a letter grade. Is there a way to make that happen? If not, I'll just change my code to do something along the lines of what you ahve posted here.

By the way, thanks for your quick help!!

 
0
 

this one works for me:

cout<<"Enter number of grade input: ";
    cin>>numgrade;
    for (int x=0;x<numgrade;x++)
    {
        cout<<"Enter a grade in letter format: ";
        cin>>usergrade;
        switch (usergrade)
        {
               case 'A':
                    totalgrade+=4;
                    break;
                 //and so on...

Okay, just saw this one. I think this is more what she's looking for, and we did talk breifly about switch statments, so I might go this route. Anyway, thank you both for your replies!!

 
0
 

Okay, I think i see where you are going with this, but there is one question I have.. Where does each grade letter get assigned the 0-4 value, or is the user doing that by entering the number?

I only ask, because I think the instructor is wanting the use to type a letter grade. Is there a way to make that happen? If not, I'll just change my code to do something along the lines of what you ahve posted here.

By the way, thanks for your quick help!!

Where cout is output to the screen, cin is input from the user. Use cin to store a value typed in by the user into a variable. Look over my program again with this in mind and you'll see what I did.

Question Answered as of 6 Years Ago by SpunkyMan, WaltP and carnage
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article