# Thread: gpa calc

1. ## 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";
cout << "Please insert the semester (Fall, Spring,...):\n";
cin >> semester;
cout << "Please insert the year:\n";
cin >> year;
cout << "Your grade point average for the" << year << semester << "semester is" << gpa;
'A'==4.0;
'B'==3.0;
'C'==2.0;
'D'==1.0;
'F'==0.0;

}```

2. 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'){
..........................
...................
....................```
then finally

Code:
```double GPA = 0.0;

3. 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()
{

cout << "Please insert your letter grade for five classes:\n";
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")
else if (gradeTwo == "B")
else if (gradeThree == "C")
else if (gradeFour == "D")
else if (gradeFive == "F")
double gpa = 0.0;
cout << "Your grade point average for the " << year << semester << " semester is " << gpa;

}```

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