Thread: gpa calc

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    gpa calc

    hi im making a gpa calculator for my class and am confused on how to add value to my grade. should i be using a swith statement instead of an if statement? here is my code:

    Code:
    #include <string>
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    //Input lettergrades for courses, compute their grade point, and ask which semester 
    
    int main()
    {
    	
    	string grade, year, semester;
    	float gpa, A, B, C, D, F;
    	
    	cout << "Please insert your letter grade for five classes:\n";
    	cin >> grade >> grade >> grade >> grade >> grade;
    	cout << "Please insert the semester (Fall, Spring,...):\n";
    	cin >> semester;
    	cout << "Please insert the year:\n";
    	cin >> year;
    	grade=A,B,C,D,F;
    	gpa=grade+grade+grade+grade+grade/(5);
    	cout << "Your grade point average for the" << year << semester << "semester is" << gpa;
    	if (grade=="A")
    		'A'==4.0;
    	else if (grade=="B")
    		'B'==3.0;
    	else if (grade=="C")
    		'C'==2.0;
    	else if (grade=="D")
    		'D'==1.0;
    	else (grade=="F")
    		'F'==0.0;
    	
    	
    		
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    I don't think your line of code were you have

    Code:
    cin>> grade >> grade....
    is going to work. I would instead write

    Code:
    cin>> gradeOne >> gradeTwo ...
    and then use if ladders to assign double values to "calculation variables" based off of the user input. Like:

    Code:
    double gradeOneCalc = 0.0;
    
    if (gradeOne == 'A'){
     gradeOneCalc = 4.0;} 
    ..........................
    ...................
    ....................
    then finally

    Code:
    double GPA = 0.0;
    
    GPA = gradeOneCalc+GradeTwoCalc..... /5

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    ok this is what i did where did i mess up?

    Code:
    #include <string>
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    //Input lettergrades for courses, compute their grade point, and ask which semester 
    
    int main()
    {
    	
    	string gradeOne, gradeTwo, gradeThree, gradeFour, gradeFive, year, semester;
    	
    	
    	cout << "Please insert your letter grade for five classes:\n";
    	cin >> gradeOne >> gradeTwo >> gradeThree >> gradeFour >> gradeFive;
    	cout << "Please insert the semester (Fall, Spring,...):\n";
    	cin >> semester;
    	cout << "Please insert the year:\n";
    	cin >> year;
    	double gradeOneCalc = 0.0;
    	double gradeTwoCalc = 0.0;
    	double gradeThreeCalc = 0.0;
    	double gradeFourCalc = 0.0;
    	double gradeFiveCalc = 0.0;
    	if (gradeOne == "A")
    		gradeOneCalc=4.0;
    	else if (gradeTwo == "B")
    		gradeTwoCalc=3.0;
    	else if (gradeThree == "C")
    		gradeThreeCalc=2.0;
    	else if (gradeFour == "D")
    		gradeFourCalc=1.0;
    	else if (gradeFive == "F")
    		gradeFiveCalc=0.0;
    	double gpa = 0.0;
    	gpa=gradeOneCalc+gradeTwoCalc+gradeThreeCalc+gradeFourCalc+gradeFiveCalc/(5.0);
    	cout << "Your grade point average for the " << year << semester << " semester is " << gpa;
    	
    	
    		
    }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Location
    Knoxville, Tennessee, United States
    Posts
    20
    Oh, I forgot to mention, the letter grades are not strings, but are type char.

    So, you would declare them like so. I like to always assign them a value for starts personally, then reassign it later. Notice the single quote marks. Year would probably be an int value like "2010", and semester should still be a string value. It's also a good habit to declare all of your values that will be used globally (throughout the entire program) right after you declare (using namespace std. So that you don't have a double GPA=0.0 in the middle of your code.

    Code:
    char gradeOne = 'a';
    char gradeTwo = 'b';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help implementing a class
    By jk1998 in forum C++ Programming
    Replies: 8
    Last Post: 04-05-2007, 03:13 PM
  2. Calc age use Date of PC
    By miziri in forum C++ Programming
    Replies: 12
    Last Post: 03-18-2006, 05:01 AM
  3. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM
  4. proofread my expos-ay
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-04-2002, 08:31 PM
  5. Basic Calc
    By Universe in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2002, 10:07 PM