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!

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

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.

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

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!!

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!!

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.

how to compute grade from inputing score, number of items, and grade it to 1.0 the highest and 3.0 the lowest grade. using switch case statement..

include <iostream>

using namespace std;
int percent(int score, int items);
int main()

{
int score, items;

cout << "enter score: ";
cin >> score;
cout << "enter items: ";
cin >> items;
int percentgrade = percent (score, items);

cout << "your percent grade is: " << percentgrade << "%!";

switch(percentgrade);

{
case '100':
  cout << "your grade is 1.0 !";
  cout << "very excellent!!!";
  break;

  case '99':
  cout << "your grade is 1.5 !";
  cout << "excellent! ";
  break;

  case '98':
  cout << "your grade is 1.4 ! ";
  cout << "good!";
  break;

  default:
  cout << "score is below passing ";
  cout << "your grade is 5.0 ! ";
  cout << "failed!";
  break;

}
return 0;

}
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.