Simple input output program C++

In summary: So, in summary, the conversation is about a homework assignment to create a program in C++ to calculate GPA. The assignment requires input of grades in the form of letters (A, B, C, D, F) and the program should assign numerical values to each grade. The user is having trouble assigning the numerical values to the input and is asking for tips. The expert suggests using one variable for the input letter grade and one for the output number grade, and using more descriptive variable names instead of x, y, z. The expert also provides an example of how to assign numerical values to the input grades and suggests using comments to explain the code and make it easier to read.
  • #1
MostlyHarmless
345
15
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?
 
Physics news on Phys.org
  • #2
if (input == 'A')
gpa = 4;
if (input == 'B')
gpa = 3;
...
 
  • #3
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?
 
  • #4
Jesse H. said:
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.
Jesse H. said:
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'.
Jesse H. said:
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')
{
   numberGrade = 4;
}
else if (letterGrade == 'B')
{
   numberGrade = 3;
}
// and so on
 
  • #5
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.
 
  • #6
Jesse H. said:
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.
 
  • #7
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;
//Asking for input from user

*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;

}
 
  • #8
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.
 
  • #9
I embedded your code in [code] [/code] tags, which makes it a little easier to read. I also removed the extra * characters.
Jesse H. said:
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;

//Asking for input from user

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];
  char letterGrades[NUMCLASSES];
  float numberGrades[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];
    cin << letterGrades[j];
  } 
  
  // Convert letter grades A, B, C, D, and F to numerical values.
  for (int j = 0; j < NUMCLASSES; j++)
  {
    if (letterGrades[j] == 'A' || letterGrades[j] == 'a' )
    {
      numberGrades[j] = 4.0;
    }

    else if (letterGrades[j] == 'B' || letterGrades[j] == 'b' ) 
    {
      numberGrades[j] = 3.0;
    }

    else if (letterGrades[j] == 'C' || letterGrades[j] == 'c' )
    {
      numberGrades[j] = 2.0;
    }

    else if (letterGrades[j] == 'D' || letterGrades[j] == 'd' ) 
    {
      numberGrades[j] = 1.0;
    }

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

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

  return 0;

}

A couple of comments...
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.
 
  • #10
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[6] = {4, 3, 2, 1, 0, 0};
// ...
int number = gradetonumber[grade - 'A'];
 
  • #11
Yeah we have not learned about loops and arrays, we are actually specifically told we can't use them in that assignment, and actually the next assignment calls for us to improve upon our code using loops and more conditional statements but once again, no arrays.
 
  • #12
Jesse H. said:
Yeah we have not learned about loops and arrays, we are actually specifically told we can't use them in that assignment, and actually the next assignment calls for us to improve upon our code using loops and more conditional statements but once again, no arrays.
but you could use math to convert a character grade 'A' through 'F' to an integer value 4 through 0, using the example I showed above, which would reduce the number of if statements.
 

What is a simple input output program in C++?

A simple input output program in C++ is a basic program that takes input from the user, performs some operations on it, and displays the result. It is often used as a starting point for learning the language and understanding how to work with user input and output.

What are the essential components of a simple input output program in C++?

The essential components of a simple input output program in C++ include a header file (e.g. ), a main function, input statement(s) (e.g. ), output statement(s) (e.g. ), and any necessary variables and operations.

How do I take user input in a simple input output program in C++?

To take user input in a simple input output program in C++, you can use the statement, followed by the variable name where you want to store the input. For example:

int x;
cin >> x;

This will prompt the user to enter a value, and the value will be stored in the variable "x".

How do I display output in a simple input output program in C++?

To display output in a simple input output program in C++, you can use the statement, followed by the value or variable you want to display. For example:

int x = 5;
cout << "The value of x is: " << x;

This will display the message "The value of x is: 5" to the user.

Can I use multiple input and output statements in a simple input output program in C++?

Yes, you can use multiple input and output statements in a simple input output program in C++. This allows you to take multiple input values and perform multiple operations, and then display the results to the user. Just make sure to use the correct syntax and data types for your input and output statements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top