We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,434 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

C++ Student Average Grade program

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!

4
Contributors
6
Replies
13 Hours
Discussion Span
5 Years Ago
Last Updated
13
Views
Question
Answered
badkid32
Newbie Poster
5 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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...
carnage
Junior Poster in Training
55 posts since Jan 2008
Reputation Points: 34
Solved Threads: 2
Skill Endorsements: 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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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. :)

SpunkyMan
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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!!

badkid32
Newbie Poster
5 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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!!

badkid32
Newbie Poster
5 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

SpunkyMan
Newbie Poster
2 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 5 Years Ago by SpunkyMan, WaltP and carnage

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0740 seconds using 2.71MB