3 Replies - 545 Views - Last Post: 24 January 2013 - 08:56 AM Rate Topic: -----

#1 verb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-January 13

C++ GPA calculator experiencing loss of entered data

Posted 24 January 2013 - 08:26 AM

Hello. I've been trying to make a GPA calculator using structs in C++, but I've been having a problem. The program compiles properly, and allows me to enter data for any given number of classes, but when the final table is printed, the name of the first class entered is replaced with that of the last class, but all the other data is the right one from the original. On top of that, all the data from the other classes defaults to 0, which then skews the average. I originally thought the data wasn't being retrieved properly, but I am able to print it right after input and it displays correctly. I then tried getting rid of the main while loop, thinking that the data might have been lost in the cycles, but writing the code in a series of straightforward if-statements yields the same problem. I would greatly appreciate any help.
Note: this was made with Microsoft Visual Studio 6.0

#include <iostream.h>
#include <apstring.cpp>
int classnum;
int counter =1;
struct classav{ //declares classaverage data type
	apstring name; //Declares classs name string
	apstring grade; //Declares grade string
	double percent; //Pwcentage double
	double GP;//Grade point for class
	double GPweight;//Weighted GP
	int isadv;//Determines whether class is AP/IB, honors, or unweighted
};
classav class1,class2,class3, class4, class5, class6, class7, class8;
 double findGP( classav x){//grade point function, determines grade point based on percentage

		if (x.percent >=97)	{ 
			 x.GP = 4.33;		
		}
		  else if (x.percent <97 && x.percent>=94){
			 x.GP=4.00;
		}
		else if (x.percent <93 && x.percent >=90){
			 x.GP=3.66;
		}
		else if (x.percent<90 && x.percent>=87){ 
			 x.GP = 3.33;
		}
		else if (x.percent <87 && x.percent>=83){
				x.GP=3.00;		
		}
		 else if (x.percent <83 && x.percent >=80){
			x.GP=2.66;
		}
	else if (x.percent<80 && x.percent>=77){ 
				x.GP = 2.33;
		}
	else if (x.percent <77 && x.percent>=73){
					x.GP=2.00;
		}
	else if (x.percent <73 && x.percent >=70){
					x.GP=2.66;
		}
	else if (x.percent<70 && x.percent>=67){ 
				x.GP = 1.33;
		}
	else if (x.percent <67 && x.percent>=63){
				x.GP=1.00;
		}
		else if (x.percent <63 && x.percent >=60){
				x.GP=0.66;
		}
		 else{ 
				x.GP = 0;
		}
		 if (x.isadv == 1){
			 x.GPweight= x.GP +1;
		 }
		 if (x.isadv == 2){
			 x.GPweight =x.GP +2;
		 }
		 else{
			 x.GPweight =x.GP;
		 }



	return x.GP;

}
double findGPweight(classav x){//determines weighted GPA

 	 if (x.isadv == 1){
			 x.GPweight= x.GP +1;
		 }
	 else if (x.isadv == 2){ ;
	 	 x.GPweight =x.GP +2;
		 }
		 else{
			 x.GPweight =x.GP;
		 }
return x.GPweight;
}

apstring findGrade(classav x){//function, determines letter grade based on percentage

	if (x.percent >=97)	{ 
			 x.grade = "A+";	
		}
		else if (x.percent <97 && x.percent>=94){
			 x.grade = "A";
		}
		else if (x.percent <93 && x.percent >=90){
		 x.grade = "A-";
		}
		else if (x.percent<90 && x.percent>=87){ 
			  x.grade = "B+";
		}
		 else if (x.percent <87 && x.percent>=83){
			 x.grade = "B";	
		}
		 else if (x.percent <83 && x.percent >=80){
			 x.grade = "B-";
		}
		else if (x.percent<80 && x.percent>=77){ 
			 x.grade = "C+";
		}
		else if (x.percent <77 && x.percent>=73){
				 x.grade = "C";
		}
		else if (x.percent <73 && x.percent >=70){
				 x.grade = "C-";
		}
		else if (x.percent<70 && x.percent>=67){ 
			 x.grade = "D+";
		}
		else if (x.percent <67 && x.percent>=63){
			 x.grade = "D";
		}
		else if (x.percent <63 && x.percent >=60){
			 x.grade = "D-";	
		}
		 else{ 
			 x.grade = "F";		
		}

	return x.grade;

}




int main(){
	cout <<"Welcome to the GPA calculator. How many  classes (1-8)do you take?" << endl;
	cin >> classnum; //Number of classes counted

	while (counter <= classnum){ //While loop, runs series of if-statements to retrieve class data
if (counter ==1){
		
			cout << "Please enter the class name" << endl;
			getline (cin,class1.name); //retrieves class name, getline used twice due to apstring problem
			getline (cin,class1.name);
			 cout << "Please enter your percent average in that class" << endl;
			 cin >> class1.percent; //Class perecnt
			 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class1.isadv;//Is the class advanced?
			  class1.GP =findGP (class1); //Refers to GP function
			   class1.GPweight =findGPweight (class1);//Refers to weighted GPA function
			 class1.grade =findGrade (class1);			//REfers to find letter grade function
	}
if (counter ==2){ //All subsequent if-statements within the loop retrive information for other classes
			classav class2;
			cout << "Please enter the class name" << endl;
			getline (cin,class2.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class2.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class2.isadv;
		  class2.GP =findGP (class2);
		   class2.GPweight =findGPweight (class2);
		  class2.grade =findGrade (class2);
	}
if (counter ==3){
			classav class3;
			cout << "Please enter the class name" << endl;
			getline (cin,class3.name);
			getline (cin,class1.name);
			 cout << "Please enter your percent average in that class" << endl;
			cin >> class3.percent;
			cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class3.isadv;
			 class3.GP =findGP (class3);
			  class3.GPweight =findGPweight (class3);
		  class3.grade =findGrade (class3);
	}
if (counter ==4){
			classav class4;
			cout << "Please enter the class name" << endl;
			getline (cin,class4.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class4.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class4.isadv;
		  class4.GP =findGP (class4);
		   class4.GPweight =findGPweight (class4);
		  class4.grade =findGrade (class4);
	}
if (counter ==5){
			classav class5;
			cout << "Please enter the class name" << endl;
			getline (cin,class5.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class5.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class5.isadv;
		  class5.GP =findGP (class5);
		   class5.GPweight =findGPweight (class5);
		  class5.grade =findGrade (class5);
	}
if (counter ==6){
			classav class6;
			cout << "Please enter the class name" << endl;		
			getline (cin,class6.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class6.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class6.isadv;
		  class6.GP =findGP (class6);
		   class6.GPweight =findGPweight (class6);
		  class6.grade =findGrade (class6);
	}
if (counter ==7){
			classav class7;
			cout << "Please enter the class name" << endl;
			getline (cin,class7.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class7.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class7.isadv;
 class7.GP =findGP (class7);
  class7.GPweight =findGPweight (class7);
		  class7.grade =findGrade (class7);
	}
if (counter ==8){
			classav class8;
			cout << "Please enter the class name" << endl;
			getline (cin,class8.name);
			getline (cin,class1.name);
		 cout << "Please enter your percent average in that class" << endl;
		 cin >> class8.percent;
		 cout <<" Is the class: \n 2.Advanced Placement/IB \n 1. Honors \n 0: Regular" << endl;
			 cin >> class8.isadv;
		  class8.GP =findGP (class8);
		   class8.GPweight =findGPweight (class8);
		 class8.grade =findGrade (class8);
	}
counter++; //Increments counter

	}


	counter =counter-1;//Subtracts one from counter to compensate for final increment at end of while loop; allows if-statements below to function properly
	cout<<counter<< endl;
classav Average;
if (counter ==1){ //If class number/counter equals one, declares Average class and performs calculations as done for individual classes above. This is done in the same manner for different numbers of classes below

	Average.name = "Average";
	Average.percent = class1.percent;
	Average.GP = class1.GP;
	Average.GPweight = class1.GPweight;
	Average.grade = findGrade(Average);
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;
		
	}
if (counter ==2){
	Average.name = "Average";
	Average.percent = (class1.percent+class2.percent)/2;
	Average.GP = (class1.GP+class2.GP)/2;
	Average.GPweight = (class1.GPweight+class2.GPweight)/2;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;
		
	}
if (counter ==3){
	Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent)/3;
	Average.GP = (class1.GP+class2.GP+class3.GP)/3;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight)/3;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;		
	}
if (counter ==4){
	Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent+class4.percent)/4;
	Average.GP = (class1.GP+class2.GP+class3.GP+class4.GP)/4;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight+class4.GPweight)/4;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << class4.name << "    " << class4.percent << "   " << class4.grade << "   " << class4.GP << "      " << class4.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;		
	}
			
	
if (counter ==5){
	Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent+class4.percent+class5.percent)/5;
	Average.GP = (class1.GP+class2.GP+class3.GP+class4.GP+class5.GP)/5;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight+class4.GPweight+class5.GPweight)/5;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << class4.name << "    " << class4.percent << "   " << class4.grade << "   " << class4.GP << "      " << class4.GPweight <<endl;
	cout << class5.name << "    " << class5.percent << "   " << class5.grade << "   " << class5.GP << "      " << class5.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;	
		
	}
if (counter ==6){
	Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent+class4.percent+class5.percent+class6.percent)/6;
	Average.GP = (class1.GP+class2.GP+class3.GP+class4.GP+class5.GP+class6.GP)/6;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight+class4.GPweight+class5.GPweight+class6.GPweight)/6;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << class4.name << "    " << class4.percent << "   " << class4.grade << "   " << class4.GP << "      " << class4.GPweight <<endl;
	cout << class5.name << "    " << class5.percent << "   " << class5.grade << "   " << class5.GP << "      " << class5.GPweight <<endl;
	cout << class6.name << "    " << class6.percent << "   " << class6.grade << "   " << class6.GP << "      " << class6.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;		
	}
if (counter ==7){
			Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent+class4.percent+class5.percent+class6.percent+class7.percent)/7;
	Average.GP = (class1.GP+class2.GP+class3.GP+class4.GP+class5.GP+class6.GP+class7.GP)/7;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight+class4.GPweight+class5.GPweight+class6.GPweight+class7.GPweight)/7;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << class4.name << "    " << class4.percent << "   " << class4.grade << "   " << class4.GP << "      " << class4.GPweight <<endl;
	cout << class5.name << "    " << class5.percent << "   " << class5.grade << "   " << class5.GP << "      " << class5.GPweight <<endl;
	cout << class6.name << "    " << class6.percent << "   " << class6.grade << "   " << class6.GP << "      " << class6.GPweight <<endl;
	cout << class7.name << "    " << class7.percent << "   " << class7.grade << "   " << class7.GP << "      " << class7.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;		
		
	}
if (counter ==8){
		Average.name = "Average";
	Average.percent = (class1.percent+class2.percent+class3.percent+class4.percent+class5.percent+class6.percent+class7.percent+class8.percent)/8;
	Average.GP = (class1.GP+class2.GP+class3.GP+class4.GP+class5.GP+class6.GP+class7.GP+class8.GP)/8;
	Average.GPweight = (class1.GPweight+class2.GPweight+class3.GPweight+class4.GPweight+class5.GPweight+class6.GPweight+class7.GPweight+class8.GPweight)/8;
	Average.grade = findGrade(Average);	
	cout << "Class    Percent   Grade   GP(Unweighted)   GP(Weighted)" << endl;
	cout << class1.name << "    " << class1.percent << "   " << class1.grade << "   " << class1.GP << "      " << class1.GPweight <<endl;
	cout << class2.name << "    " << class2.percent << "   " << class2.grade << "   " << class2.GP << "      " << class2.GPweight <<endl;
	cout << class3.name << "    " << class3.percent << "   " << class3.grade << "   " << class3.GP << "      " << class3.GPweight <<endl;
	cout << class4.name << "    " << class4.percent << "   " << class4.grade << "   " << class4.GP << "      " << class4.GPweight <<endl;
	cout << class5.name << "    " << class5.percent << "   " << class5.grade << "   " << class5.GP << "      " << class5.GPweight <<endl;
	cout << class6.name << "    " << class6.percent << "   " << class6.grade << "   " << class6.GP << "      " << class6.GPweight <<endl;
	cout << class7.name << "    " << class7.percent << "   " << class7.grade << "   " << class7.GP << "      " << class7.GPweight <<endl;
	cout << class8.name << "    " << class8.percent << "   " << class8.grade << "   " << class8.GP << "      " << class8.GPweight <<endl;
	cout << Average.name << "    " << Average.percent << "   " << Average.grade << "   " << Average.GP << "      " << Average.GPweight <<endl;		
	}

								



return 0;
	}
		





Is This A Good Question/Topic? 0
  • +

Replies To: C++ GPA calculator experiencing loss of entered data

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: C++ GPA calculator experiencing loss of entered data

Posted 24 January 2013 - 08:34 AM

Part of your problem is being caused by your use of global variables.

Quote

main.cpp||In function ‘int main()’:|
main.cpp|157|warning: declaration of ‘class2’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|170|warning: declaration of ‘class3’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|183|warning: declaration of ‘class4’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|196|warning: declaration of ‘class5’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|209|warning: declaration of ‘class6’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|222|warning: declaration of ‘class7’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
main.cpp|235|warning: declaration of ‘class8’ shadows a global declaration [-Wshadow]|
main.cpp|17|warning: shadowed declaration is here [-Wshadow]|
||=== Build finished: 0 errors, 17 warnings ===|

You have global variables with these names, then you try to declare new instances inside main.

Also why are you including a source file (#include <apstring.cpp>)?

You should also consider finding an updated compiler. Any compiler that allows the use of "#include <iostream.h>" is very outdated and should be replaced with a more modern standard compliant compiler.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 verb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-January 13

Re: C++ GPA calculator experiencing loss of entered data

Posted 24 January 2013 - 08:40 AM

I see. I'll try fixing that. To answer your question, apstring.cpp is used to compute strings. I use it place of string.h, as I was told the other had problems parsing substrings. About the compiler, I will take that into account.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,737
  • Joined: 25-December 09

Re: C++ GPA calculator experiencing loss of entered data

Posted 24 January 2013 - 08:56 AM

Normally you don't "include" a source file you add it to your project to be compiled.

Quote

I use it place of string.h, as I was told the other had problems parsing substrings.

The string.h header file is a C header for dealing with C-strings, and not C++ strings. The primary reason you would need this class is because your ancient compiler doesn't properly support the standard C++ string class. The C++ standard string class handles substrings quite well.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1