Simple input output program C++

Click For Summary

Discussion Overview

The discussion revolves around a homework assignment requiring the creation of a C++ program to calculate GPA based on letter grades (A, B, C, D, F) and corresponding credit hours. Participants explore various coding approaches, variable naming conventions, and logic for handling user input.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks advice on how to assign numerical values to letter grades input by the user, specifically how to treat 'A' as 4.
  • Another participant suggests using conditional statements to assign values based on the input letter grade.
  • Concerns are raised about the use of multiple variables (x, y, z) for input and whether it is necessary to define each letter grade for every variable.
  • Suggestions are made to use more descriptive variable names instead of generic ones like x, y, or z, emphasizing the importance of readability in code.
  • One participant clarifies that the assignment requires handling five inputs, and they mention that comments in their code help explain their logic to the grader.
  • Another participant points out issues with the original code, such as the presence of asterisks and suggests improvements for clarity and structure.
  • A later reply introduces the idea of using arrays to simplify the code and improve efficiency, while also addressing case sensitivity in letter grade inputs.
  • Concerns are raised about handling invalid letter grades, indicating a need for additional input validation logic.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of multiple variables versus using arrays, and there is no consensus on the best approach to handle input validation for letter grades. The discussion remains unresolved regarding the optimal structure and readability of the code.

Contextual Notes

Limitations include potential confusion over variable naming conventions, the handling of invalid inputs, and the clarity of the original code structure. Some participants suggest improvements without reaching a definitive solution.

MostlyHarmless
Messages
344
Reaction score
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
if (input == 'A')
gpa = 4;
if (input == 'B')
gpa = 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?
 
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
 
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.
 
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.
 
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

*count <<"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.
*count << "Your GPA is:"<< ((c1 * g1)+(c2 * g2)+(c3 * g3)+(c4 * g4)+(c5 * g5))/(c1+c2+c3+c4+c5)<<"\n";

return 0;

}
 
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.
 
I embedded your code in [code[/color]] [/code[/color]] 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.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K