I have written the following code to try to get my GPA calculator program to work. I am not allowed to use arrays. I only know what I am doing from the videos I have watched on youtube and the slideshows that we get from our instructor. Unfortunately, the slideshow this week does not cover using While and If statements.

Any help would be appreciated.

The goal of this is to find out the GPA of various courses.
Each course has a specific number of credit hours.
I am to prompt for a grade letter. The grade letter gets converted to a number letter, and then I am supposed to figure out the GPA.

// 

#include <iostream>
using namespace std;

void Error()
{
	cout << "Not a valid letter grade. Please check your letter grade and try again." << endl;
}

//need to find the grade point average for the semester

void main()
{
	float Sum = 0.00f;
	int Counter = 0;
	char LetterGrade = 0;
	float number = 0.00f;

//GetLetterGrade Module
	
	cout << "Enter Letter Grade: ";
	cin >> LetterGrade;

	while(LetterGrade != 0000)
	{
		Sum += LetterGrade;
		Counter += 1;
		cout << "Enter Letter Grade: ";
		cin >> number;
	}

//Convert Letter Grades to Numeric values
	switch(LetterGrade)
	   {
		
		case 'f':
		case 'F': number = 0.0;
			cout << "0.0";
			break;
		case 'd':
		case 'D': number = 1.0;
			break;
		case 'c':
		case 'C': number = 2.0;
			break;
		case 'b':
		case 'B': number = 3.0;
			break;
		case 'a':
		case 'A': number = 4.0;
			break;
		default: Error();
		      break;
	   }


	float Average = 0.00f;
	
//CalcAvgTemp Module    
	Average = Sum/Counter;

//PrintAvgTemp Module
	cout << "The Grade Point Average is " << Average << endl;
}

First off, I am not trying to become a programmer, so I will not be delving into the C++ world and with that I do not claim to know what I am doing. However, I do need to solve this problem.

So here is my problem solving method:

1. Identify the problem:
Create a program to identify the GPA for the semester.
2. Understand the problem:
Grades are given in letters
Convert the grades to numerical values (A=4.0, B=3.0, etc.,)
Grade point average is figured by units of credit
Example: History = B = 3 Units
have to use a trip value to stop the processing
have to use case structure to find the grade points.


The rest is algorithmic method, so i won;t post that here. But basically, I have the While command in there for the loop, but when I enter the second letter grade, my application goes into constant loop.

Second I am not sure how to "connect" the While statement with the Switch.

Your second input for Letter Grade wants to input into number (a float) rather than a character.

And what values do you expect to get with Sum += LetterGrade; when Sum is a float and LetterGrade is a character?

Second: put the switch statement inside the loop, with all the code needed to process each input.

Your second input for Letter Grade wants to input into number (a float) rather than a character.

So should I change the code to say Char number = 0

And what values do you expect to get with Sum += LetterGrade; when Sum is a float and LetterGrade is a character?

It worked for the other assignment that I did where we had to ad the sum of multiple values. I am not understanding how to get all of the entries to add up.

Second: put the switch statement inside the loop, with all the code needed to process each input.

I have no idea what this means.

program statements have to be written in logical sequence. For example you can not add LetterGrade to Sum before you know the value of LetterGrade. And LetterGrade has to be converted from A to F into numeric value befor it can be added to Sum.

char LetterGrade = 'A';
int number;
int Sum = 0;
while(LetterGrade != '0')
{
    cout << "Enter Letter Grade: ";
    cin >> LetterGrade;
    switch(LetterGrade)
    {
      case 'A': number = 100; break;
      // other case statements go here for each grade
    }
    Sum += number;
    Counter++;
}

Edited 6 Years Ago by Ancient Dragon: n/a

I have made a few changes, broke out the modules and included your updated code (thank you very much Ancient Dragon)

Now the GPA, Sum, CreditHours are undeclared identifiers. Do I decalre them in Main?
Also, how do I get the CreditHours to add up? (guess this won;t matter until I declare the others)

#include <iostream>
using namespace std;

void GetLetterGrade();
void Error();
void CalcGPA();
void PrintGPA();

void main()
{

}

void GetLetterGrade()
{
	char LetterGrade = 'A';
	int CreditHours = 0;
	double Sum = 0;
	double number = 0;
	while(LetterGrade != 'E')
    {
		cout << "Enter Letter Grade: ";
		cin >> LetterGrade;
	
		cout << "Enter Credit Hours for this course: ";
		cin >> CreditHours;

		switch(LetterGrade)
	    {
		case 'a':
		case 'A': number = 4.0;
			break;
		case 'b':
		case 'B': number = 3.0;
			break;
		case 'c':
		case 'C': number = 2.0;
			break;
		case 'd':
		case 'D': number = 1.0;
			break;
		case 'f':
		case 'F': number = 0.0;
			break;
		default: Error();
		      break;
	    }
		Sum += number;
		
	}
}
void Error()
{
	cout << "Not a valid letter grade. Please check your letter grade and try again." << endl;
}

void CalcGPA()
{
	GPA = Sum/CreditHours;
}

void PrintGPA()
{
	cout << "The Grade Point Average is " << GPA << endl;
}

replacing Sum didn;t work, I must be missing something in Main.

Okay, I have made some more changes, specifically removing the GetGradeLetter module and now I am able to compile the program (at leastwithout the calculation part anyway) and it is now accepting my Grade letter and Credit Horus, but the Credit Hours will need a reentry statement so i guess i will work on that, any help is still appreciated.

#include <iostream>
using namespace std;

int GetLetterGrade();
void Error();
void CalcGPA();
void PrintGPA();

void main()
{

    char LetterGrade = 'A';//value entered will be a character
	int CreditHours = 0;//value entered will be a whole number
	int Sum = 0;//value of sum will be a whole number
	int number = 0;//value of number will be a whole number
	
	cout << "Enter Letter Grade: " << endl ;
	cin >> LetterGrade;

	cout << "Enter Credit Hours for this course: " << endl;
	cin >> CreditHours;
	
	while(LetterGrade != 'E')//
    {
		
		cout << "Enter Letter Grade: " << endl;
		cin >> LetterGrade;
		
		switch(LetterGrade)
	    {
		case 'a':
		case 'A': number = 4;
			break;
		case 'b':
		case 'B': number = 3;
			break;
		case 'c':
		case 'C': number = 2;
			break;
		case 'd':
		case 'D': number = 1;
			break;
		case 'f':
		case 'F': number = 0;
			break;
		default: Error();
		      break;
	    }
	}
}
void Error()
{
	cout << "Not a valid letter grade. Please check your letter grade and try again." << endl;
}

/*void CalcGPA()
{
	GPA = Sum/CreditHours;
}

void PrintGPA()
{
	cout << "The Grade Point Average is " << GPA << endl;
}*/

Now I have a problem. I have added another While statement for the Credit Hours on the same level as GetGradeLevel and when my program Debugs I am asked for the Letter Grade. I hit enter and then it asks me for the Credit Hours. I enter credit hours and hit enter and then it constantly asks me for the credit hours. I was on a roll, but I am clueless at this point. How do I get the program to ask me the Grade Letter then the Credit hours, then the Grade Letter, then the Credit hours...all the while these are addign up in the background?

#include <iostream>
using namespace std;

int GetLetterGrade();
void Error();
void CalcGPA();
void PrintGPA();

void main()
{

    char LetterGrade = 'A';//value entered will be a character
	int CreditHours = 0;//value entered will be a whole number
	int Sum = 0;//value of sum will be a whole number
	int number = 0;//value of number will be a whole number
	
	cout << "Enter Letter Grade: " << endl ;
	cin >> LetterGrade;

	cout << "Enter Credit Hours for this course: " << endl;
	cin >> CreditHours;

	while(CreditHours != 0)
	{
		Sum += CreditHours;
		cout << "Enter Credit hours for this course: ";
		cin >> CreditHours;
	}

	while(LetterGrade != '0')//
    {
		
		cout << "Enter Letter Grade: " << endl;
		cin >> LetterGrade;
		
		switch(LetterGrade)
	    {
		case 'a':
		case 'A': number = 4;
			break;
		case 'b':
		case 'B': number = 3;
			break;
		case 'c':
		case 'C': number = 2;
			break;
		case 'd':
		case 'D': number = 1;
			break;
		case 'f':
		case 'F': number = 0;
			break;
		default: Error();
		      break;
	    }
	}
}
void Error()
{
	cout << "Not a valid letter grade. Please check your letter grade and try again." << endl;
}

/*void CalcGPA()
{
	GPA = Sum/CreditHours;
}

void PrintGPA()
{
	cout << "The Grade Point Average is " << GPA << endl;
}*/

Use some thing like that definitely it will work for you.
You want that program to ask user the Grade Letter then the Credit hours, then the Grade Letter, then the Credit hours...all the while these are addign up in the background?
Your problem is that your using two while 1st one for Credit hour and 2nd one for Grade letter which is worng you have to use a single while loop some thing like this

void main()
{

    char LetterGrade = 'A';//value entered will be a character
	int CreditHours = 0;//value entered will be a whole number
	int Sum = 0;//value of sum will be a whole number
	int number = 0;//value of number will be a whole number
	
	cout << "Enter Letter Grade: " << endl ;
	cin >> LetterGrade;

	cout << "Enter Credit Hours for this course: " << endl;
	cin >> CreditHours;

	while(CreditHours != 0 && LetterGrade != '0')
	{
		
		cout << "Enter Credit hours for this course: ";
		cin >> CreditHours;
	
		
		cout << "Enter Letter Grade: " << endl;
		cin >> LetterGrade;

		Sum += CreditHours;
		
		switch(LetterGrade)
	    {
		case 'a':
		case 'A': number = 4;
			break;
		case 'b':
		case 'B': number = 3;
			break;
		case 'c':
		case 'C': number = 2;
			break;
		case 'd':
		case 'D': number = 1;
			break;
		case 'f':
		case 'F': number = 0;
			break;
		default: Error();
		      break;
	    }
	}
}

how can we take -A as input of grade and assign them the grade points as 4.00
so that user input is -A,-B,B+ or any character with sing ....... and we calculate his/her GPA on the basis of these grades>>>???

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

{
     
     cout<<"\n$$$$$$$$$$$$$$$$$ Absar Mazhar BI-113004 $$$$$$$$$$$$$$$$"<<endl;
     cout<<"\nThis program will calculate the GPA and CGPA of your current semester\n\n";
     char opt;    
     int x;
     float gp,rc,sch,tgpa;
     float gpa=0,cgpa=0,tsch=0;
    
do
  {  
     
     cout<<"Press 1 if you are an Undergraduate Stutdent \n\n";
     cout<<"Press 2 if you are a Graduate Stutdent \n\n";
     cin>>x;
     cout<<endl;
     switch (x)
     {
     case 1:
                    {
                                              
                       cout<<"You are an Undergraduate student \n\n"; // output of enrolled program
                       cout<<"Following is the grading policy of the University \n\n"; // grading policy display
                       cout<<"A=4.00"<<endl;
                       cout<<"A-=3.67"<<endl;
                       cout<<"B+=3.33"<<endl;
                       cout<<"B=3.00"<<endl;
                       cout<<"B-=2.67"<<endl;
                       cout<<"C+=2.33"<<endl;
                       cout<<"C=2.00"<<endl;
                       cout<<"C-=1.67"<<endl;
                       cout<<"D+=1.33"<<endl;
                       cout<<"D=1.00"<<endl;
                       cout<<"F=0.00"<<endl;
                       
                       cout<<"\nEnter number of registered courses : "; // get the number of registered courses from a student
                       cin>>rc;
                       for (int i=1;i<=rc;i++)
                    {
                       float egp[i]; // egp is the gpa of each registered course 
                       cout<<"\nEnter the Credit Hours of your "<<i<< "  course : ";
                       cin>>sch; // get credit hours of (i) course 
                       cout<<"\nEnter the grade points of your "<<i<< "  course : ";
                       cin>>gp; // get grades earned in this (i) course
                       egp[i]=(sch*gp); // gpa of (i) course is calculated as
                       cout<<endl;
                       tsch=tsch+sch; // calculate total credit hours 
                       gpa=gpa+egp[i]; // get sum of gpa's in all courses
                       tgpa=gpa/tsch; // GPA is calculated as gpa by total credit hours
                       cgpa=tgpa; // CGPA is equal to GPA as we are considering only 1 semester
                    }
                       cout<<"Your total Credit Hours in this semester are "<<tsch<<endl<<endl;
                       cout<<"Your GPA of all registered courses in this semester is  "<<tgpa<<endl<<endl;
                       cout<<"Your CGPA of all registered courses in this semester is  "<<cgpa<<endl<<endl;
                       break;
                    }

     case 2:
                    {
                       cout<<"You are a Graduate student \n\n";// output of enrolled program
                       
                       cout<<"Following is the grading policy of the University \n\n"; // grading policy display
                       cout<<"A=4.00"<<endl;
                       cout<<"A-=3.67"<<endl;
                       cout<<"B+=3.33"<<endl;
                       cout<<"B=3.00"<<endl;
                       cout<<"B-=2.67"<<endl;
                       cout<<"C+=2.33"<<endl;
                       cout<<"C=2.00"<<endl;
                       cout<<"C-=1.67"<<endl;
                       cout<<"D+=0.00"<<endl;
                       cout<<"D=0.00"<<endl;
                       cout<<"F=0.00"<<endl;
                       
                       cout<<"\nEnter number of registered courses : "; // get the number of registered courses from a student
                       cin>>rc;
                       for (int i=1;i<=rc;i++)
                    {
                       float egp[i]; // egp is the gpa of each registered course
                       cout<<"\nEnter the Credit Hour of your "<<i<< "  course : ";
                       cin>>sch; // get credit hours of (i) course 
                       cout<<"\nEnter the grade points of your "<<i<< "  course : ";
                       cin>>gp; // get grades earned in this (i) course
                       egp[i]=(sch*gp); // gpa of (i) course is calculated as
                       cout<<endl;
                       tsch=tsch+sch; // calculate total credit hours 
                       gpa=gpa+egp[i]; // get sum of gpa's in all courses
                       tgpa=gpa/tsch; // GPA is calculated as gpa by total credit hours
                       cgpa=tgpa; // CGPA is equal to GPA as we are considering only 1 semester
                    }
                       cout<<"Your total Credit Hours in this semester are "<<tsch<<endl<<endl;
                       cout<<"Your GPA of all registered courses in this semester is  "<<tgpa<<endl<<endl;
                       cout<<"Your CGPA of all registered courses in this semester is  "<<cgpa<<endl<<endl;
                       break;
                    }                      
     default:
                    {
                           cout<<"\n\nPlease choose the right program of your enrollment.\n\n";        
                    }
        
        }
        
            cout<<"Do you want to choose again ? : ";
            cin>>opt;
            system("cls");
            }     
            while(opt=='y' || opt=='Y');     
            
            cout<<"\n\n\n$$$$$$$$$$$$$$$$$$$$ End Of Program $$$$$$$$$$$$$$$$$$$$$"<<endl<<endl<<endl;
            system ("pause");
            return 0;
}
This article has been dead for over six months. Start a new discussion instead.