# Simple input output program C++

MostlyHarmless
I have a homework assignment that requires me to write a program in c++ to calculate GPA. The assignment wants then input of the grade to be in terms of just A B C, etc.. I'm just having trouble assigning a number value to the user input ie when the user inputs an A for their grade, I need the program to treat it as a 4. Any tips?

jacket
if (input == 'A')
gpa = 4;
if (input == 'B')
gpa = 3;
.....

MostlyHarmless
So if I'm designated the input as a variable I.e

cin >>x>>y>>z;

Then I would have to define A B C D or F for every input?

So I would do

if (input=='A')
x=4; etc.. for every letter grade and every variable, x, y, z?

Mentor
So if I'm designated the input as a variable I.e

cin >>x>>y>>z;
Why are there three variables? Also, the variable name should be suggestive of what it will be used for. Instead of x, y, or z, I would use one variable: letterGrade, of type char.
Then I would have to define A B C D or F for every input?
No, but you have to have logic that checks the value of the input variable to see if it is 'A', 'B', 'C', 'D', or 'F'.
So I would do

if (input=='A')
x=4; etc.. for every letter grade and every variable, x, y, z?

Something like this:
Code:
if (letterGrade == 'A')
{
}
{
}
// and so on

MostlyHarmless
Thanks I got out figured out a little bit ago, I was just choosing x, y, z as an example my actual variables were c1-c5 and g1-g5.

Mentor
Thanks I got out figured out a little bit ago, I was just choosing x, y, z as an example my actual variables were c1-c5 and g1-g5.
Why do you think you need 10 variables? All you need are two - one for the input letter grade and one for the output number grade.

Also, c1, c2, etc. are not good names for variables, and aren't any better than x, y, or z. They don't give the reader of the code any idea what they are being used for. The compiler could care less what you call them, as long as they are syntactically valid, but people reading your code need any help you can give them to understand what the code is doing. If your instructor is on the ball, he or she will ding you for not choosing easily understood variable names.

MostlyHarmless
Sorry, I wasn't clear, the assignment called for the program to calculate GPA based on 5 inputs. And in my code I made comments that told the grader what I was doing and what the variables stood for.

Here is my code, its already been submitted but when asking for help in writing it the TA did pick a little at my "structure" but wasn't clear on what exactly I could improve on. Is there any tips you could suggest about cleaning up the code making it easier to read?

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

{

//Defining variables that will be used during code. Where c stands for credit hours and g stands for the corresponding grade
float c1, c2, c3, c4, c5;
char g1, g2, g3, g4, g5;

*cout <<"Please enter the number of credit hours for each class followed by the grade obtained in each class:";

// Input is in the following form: 3 4 3 4 3 A C B A B.*

*cin >> oc1>> c2>> c3>> c4>> c5>> g1>> g2>> g3>> g4>> g5;

//Defining the the grade inputs A, B, C, D, and F in terms of a numerical value.

*if (g1 == 'A') g1=4;
*if (g1 == 'B') g1=3;
*if (g1 == 'C') g1=2;
*if (g1 == 'D') g1=1;
*if (g1 == 'F') g1=0;

*if (g2 == 'A') g2=4;
*if (g2 == 'B') g2=3;
*if (g2 == 'C') g2=2;
*if (g2 == 'D') g2=1;
*if (g2 == 'F') g2=0;

*if (g3 == 'A') g3=4;
*if (g3 == 'B') g3=3;
*if (g3 == 'C') g3=2;
*if (g3 == 'D') g3=1;
*if (g3 == 'F') g3=0;

*if (g4 == 'A') g4=4;
*if (g4 == 'B') g4=3;
*if (g4 == 'C') g4=2;
*if (g4 == 'D') g4=1;
*if (g4 == 'F') g4=0;

*if (g5 == 'A') g5=4;
*if (g5 == 'B') g5=3;
*if (g5 == 'C') g5=2;
*if (g5 == 'D') g5=1;
*if (g5 == 'F') g5=0;*

//Calculating and printing the final GPA.
*cout << "Your GPA is:"<< ((c1 * g1)+(c2 * g2)+(c3 * g3)+(c4 * g4)+(c5 * g5))/(c1+c2+c3+c4+c5)<<"\n";

return 0;

}

MostlyHarmless
I'm not sure where the asterisks came from, other than in my multiplication, those shouldn't be there, I imagine its just from several copy and pastes from different formats.

Mentor
I embedded your code in [code] [/code] tags, which makes it a little easier to read. I also removed the extra * characters.
Sorry, I wasn't clear, the assignment called for the program to calculate GPA based on 5 inputs. And in my code I made comments that told the grader what I was doing and what the variables stood for.

Here is my code, its already been submitted but when asking for help in writing it the TA did pick a little at my "structure" but wasn't clear on what exactly I could improve on. Is there any tips you could suggest about cleaning up the code making it easier to read?

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

{

//Defining variables that will be used during code. Where c stands for credit hours and g stands for the corresponding grade
float c1, c2, c3, c4, c5;
char g1, g2, g3, g4, g5;

cout <<"Please enter the number of credit hours for each class followed by the grade obtained in each class:";

// Input is in the following form: 3 4 3 4 3 A C B A B.

cin >> oc1>> c2>> c3>> c4>> c5>> g1>> g2>> g3>> g4>> g5;

//Defining the the grade inputs A, B, C, D, and F in terms of a numerical value.

if (g1 == 'A') g1=4;
if (g1 == 'B') g1=3;
if (g1 == 'C') g1=2;
if (g1 == 'D') g1=1;
if (g1 == 'F') g1=0;

if (g2 == 'A') g2=4;
if (g2 == 'B') g2=3;
if (g2 == 'C') g2=2;
if (g2 == 'D') g2=1;
if (g2 == 'F') g2=0;

if (g3 == 'A') g3=4;
if (g3 == 'B') g3=3;
if (g3 == 'C') g3=2;
if (g3 == 'D') g3=1;
if (g3 == 'F') g3=0;

if (g4 == 'A') g4=4;
if (g4 == 'B') g4=3;
if (g4 == 'C') g4=2;
if (g4 == 'D') g4=1;
if (g4 == 'F') g4=0;

if (g5 == 'A') g5=4;
if (g5 == 'B') g5=3;
if (g5 == 'C') g5=2;
if (g5 == 'D') g5=1;
if (g5 == 'F') g5=0;

//Calculating and printing the final GPA.
cout << "Your GPA is:"<< ((c1 * g1)+(c2 * g2)+(c3 * g3)+(c4 * g4)+(c5 * g5))/(c1+c2+c3+c4+c5)<<"\n";

return 0;

}

OK, now I see why you have all the variables. I don't know whether you have learned about arrays yet, but if so, this is how I would do the problem.
Code:
#include <iostream>
using namespace std;
const int NUMCLASSES = 5;

int main ()

{

float creditHours[NUMCLASSES];
float totalCredits = 0.0;
float GPA = 0.0;

// Ask for input from user

cout <<"Enter the number of credit hours for each class followed by the grade obtained in each class:";

// Input is in the following form: <credits> <grade> <credits> <grade> ...
for (int j = 0; j < NUMCLASSES; j++)
{
cin << creditHours[j];
}

// Convert letter grades A, B, C, D, and F to numerical values.
for (int j = 0; j < NUMCLASSES; j++)
{
{
}

{
}

{
}

{
}

else
{
}
}

// Calculate the final GPA.
for (j = 0; j < NUMCLASSES; j++)
{
totalCredits += creditHours[j];
}
GPA = GPA / totalCredits;

// Display the GPA.
cout << "Your GPA is:"<< GPA <<"\n";

return 0;

}

I added logic so that the user could input the letter grades as either uppercase or lower case letters.

I don't have logic to take into account an input of invalid letter grades -- grades other than A, B, C, D, or F (in upper or lower case). A more careful program would check the input letter for validity.

If you don't know about arrays, you have to use a slew of variables. OTOH, arrays make it simple to deal with large quantities of information, all sharing essentially the same name plus and index.

Homework Helper
If you could take input of data one pair (credit hours, grade) at a time, you could use a loop and arrays to store the data, which would make the code simpler. To convert 'A' through 'F' to number values, you could use math, assuming that "grade" contains a letter, then

int number = 4 + (int) 'A' - (int)grade;
if(number == -1) number = 0; /* fix the case for 'F' */

or use an array and indexing:

int gradetonumber = {4, 3, 2, 1, 0, 0};
// ...