-
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;
}
-
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
-
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;
}
-
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';