1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple input output program C++

  1. Sep 16, 2013 #1
    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?
     
  2. jcsd
  3. Sep 16, 2013 #2
    if (input == 'A')
    gpa = 4;
    if (input == 'B')
    gpa = 3;
    .....
     
  4. Sep 16, 2013 #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?
     
  5. Sep 16, 2013 #4

    Mark44

    Staff: Mentor

    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.
    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'.
    Something like this:
    Code (Text):

    if (letterGrade == 'A')
    {
       numberGrade = 4;
    }
    else if (letterGrade == 'B')
    {
       numberGrade = 3;
    }
    // and so on
     
     
  6. Sep 16, 2013 #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.
     
  7. Sep 16, 2013 #6

    Mark44

    Staff: Mentor

    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.
     
  8. Sep 17, 2013 #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;

    }
     
  9. Sep 17, 2013 #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.
     
  10. Sep 17, 2013 #9

    Mark44

    Staff: Mentor

    I embedded your code in [code] [/code] tags, which makes it a little easier to read. I also removed the extra * characters.
    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 (Text):
    #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.
     
  11. Sep 17, 2013 #10

    rcgldr

    User Avatar
    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[6] = {4, 3, 2, 1, 0, 0};
    // ...
    int number = gradetonumber[grade - 'A'];
     
  12. Sep 17, 2013 #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.
     
  13. Sep 17, 2013 #12

    rcgldr

    User Avatar
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Simple input output program C++
Loading...