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!

Input/output using loops c++

  1. Sep 24, 2013 #1
    1. The problem statement, all variables and given/known data

    I'm to write a program that calculates GPA using loops to allow numerous inputs and using Error checking to be sure that inputs are valid.

    I'm having trouble figuring out how to make my loop repeat if a the input is invalid.
    And I'm also having a little trouble with the stop condition, currently I have the loop break if the user inputs -1 for both of the requested inputs, but what I wanted is for the loop to break if the Grade input is -1 and then not continue to the Credit hour input.

    *I forgot to add, I'm not allowed to use arrays in this assignment.*
    2. Relevant equations



    3. The attempt at a solution

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

    {

    //Defining variables that will be used during code.
    float CreditHours;
    int LetterGrade;
    float Total;
    float TotalCredits = 0;
    float TotalPoints = 0;
    //Asking for input from user

     cout <<"Please enter the grade for your class: 4 for A, 3 for B, 2 for C, 1 for D, 0 for F,  or '-1' when you're $

       cin >> LetterGrade;
    // Logic to ensure valid letter grade input
    // if (LetterGrade =
     cout << "Please enter the credit hours for the previously entered grade: \n";
     
       cin >> CreditHours;

    //initializing the loop for more grade inputs
    //FIX LOOP
     while (LetterGrade != -1)

    {
      //Updating Totals
      Total = LetterGrade * CreditHours;
      TotalPoints = TotalPoints +  Total;
      TotalCredits = TotalCredits + CreditHours;
     

     cout << "Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when you're d$

      cin >> LetterGrade;

     cout << "Please enter the credit hours for the previously entered grade: \n";

      cin >> CreditHours;
    //Incomplete/Questionable
     //if (CreditHours <= 0)



     // cout << "Please be sure your Credit Hours add up to a positive, non-zero value\n";
    }

    if (TotalCredits > 0)

    {
    //Calculating and printing the final GPA.
      float gpa = TotalPoints / TotalCredits;

      cout << "Your GPA is:"<< gpa <<endl;
    }

    return 0;

    }
     

    Here is my test output, which you can see IS calculating the correct GPA (Hooray), however, I still need to implement the error checking logic. And I will probably change the input method to using A B C D F, rather than 4 3 2 1 0, I just did it this way for simplicities sake for the moment.

    Code (Text):
    Please enter the grade for your class: 4 for A, 3 for B, 2 for C, 1 for D, 0 for F,  or '-1' when you're done inputting grades:
    4
    Please enter the credit hours for the previously entered grade:
    3
    Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when you're done inputting grades:
    4
    Please enter the credit hours for the previously entered grade:
    3
    Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when you're done inputting grades:
    -1
    Please enter the credit hours for the previously entered grade:
    -1
    Your GPA is:4
     
    Last edited: Sep 24, 2013
  2. jcsd
  3. Sep 24, 2013 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    In the same way it repeats if the input is valid. Check the input.

    There are two concepts how to get out of the loop:
    - use a break statement
    - use a conditional for the second question (ask only of the answer for the first question is valid)

    I would include the first class in the loop, and use a do-while loop, by the way.
    Indentation looks messy, but that might be a result of the code-tags here.
     
  4. Sep 24, 2013 #3
    Code (Text):
    #include <iostream>
    using namespace std;
    int main ()

    {

    //Defining variables that will be used during code.
    float CreditHours;
    int LetterGrade = 0;
    float Total;
    float TotalCredits = 0;
    float TotalPoints = 0;

    while (LetterGrade != -1)
      {

      //Updating Totals
       Total = LetterGrade * CreditHours;
       TotalPoints = TotalPoints +  Total;
       TotalCredits = TotalCredits + CreditHours;

            //Requesting grade input from user
            cout << "Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when y$

            //Nested loop to force valid input
                while (true)
                     {
                     cin >> LetterGrade;
                     if ((LetterGrade > 4) || (LetterGrade < -1))
                     cout << "Please enter a valid input (0, 1, 2, 3, or 4):\n";
                     else
                         break;
                     }      //Close nested loop

            // Loop break condition
                if (LetterGrade != -1)
                    {
                    //Requesting credit hour input from user
                    cout << "Please enter the credit hours for the previously entered grade: \n";

                    //Nested loop(Inside if statement> inside main loop) to force valid input
                      while (true)
                            {
                            cin >> CreditHours;
                            if ((CreditHours > 5) || (CreditHours < 1))
                            cout << "Please enter a valid value for your credit hours!\n";
                            else
                                break;
                             }      //Close nested loop


                     } //Close  conditional if statement
       }    //Close main loop




    //Calculating and printing the final GPA.
      float gpa = TotalPoints / TotalCredits;

      cout << "Your GPA is:"<< gpa << endl;

    return 0;

    }

     
    This is what I ended up with, and it seems to be working fine. Is the indentation still messy?
     
  5. Sep 24, 2013 #4

    rcgldr

    User Avatar
    Homework Helper

    Why is the first cout indented and the first while(true) indented yet again when they are not part of an outer if or loop?
     
  6. Sep 24, 2013 #5

    Mark44

    Staff: Mentor

    Indentation is a little better, but could be improved. One typical practice for the braces in a for or while loop or an if-else statement block is to put the opening brace immediately below the f in for or the w in while, like so:
    Code (Text):

    while (<something)
    {
       statement1;
       statement2;
       ...
    }
    Some of your indentation makes no sense. The basic idea is that statements that execute the same number of types have the same indentation. For example, you have this in your code:
    Code (Text):
    TotalCredits = TotalCredits + CreditHours;

         //Requesting grade input from user
         cout << "Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when y$

         //Nested loop to force valid input
             while (true)
                  {
                  cin >> LetterGrade;
                  if ((LetterGrade > 4) || (LetterGrade < -1))
                  cout << "Please enter a valid input (0, 1, 2, 3, or 4):\n";
                  else
                      break;
                  }
    I would do it like this:
    Code (Text):
    TotalCredits = TotalCredits + CreditHours;

    //Requesting grade input from user
    cout << "Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when y$

    //Nested loop to force valid input
    while (true)
    {
         cin >> LetterGrade;
         if ((LetterGrade > 4) || (LetterGrade < -1))
         {
              cout << "Please enter a valid input (0, 1, 2, 3, or 4):\n";
         }
         else
         {
              break;
         }
         <other statements>
    }
     
    Notice that I added braces in your if statement. Although they're not necessary if there is only a single statement in the body of the if, a very common error creeps in when you add another statement and forget that you now need a pair of braces.

    They way you have your outer while loop formatted, it's easy to overlook the fact that what you identify as "loop break condition" is actually part of the while loop. When code is properly indented, it's easy to see what's in the body of a control structure (e.g., loop or branch). Without proper indentation, it's difficult to grasp what's going on.
     
  7. Sep 24, 2013 #6
    I'm not hip to the coding format. :)

    The reason that made sense to me, and forgive my terminology, because the while loop is the input statement that is being requested by the proceeding output statement.

    Basically I went through and indented it so that it was clear to me what followed what. Oblivious to the norms of programming.
     
  8. Sep 25, 2013 #7

    Mark44

    Staff: Mentor

    If one statement follows another, and both execute the same number of times, their indentation level should be the same. For this reason, these three statements should be at the same level:
    Code (Text):
    TotalCredits = TotalCredits + CreditHours;
    cout << "Please enter the grade for your class: 4 for A, 3 for B, 2 for  C, 1 for D, 0 for F, or -1 when y$
    while (true)
    {
       ...
    }
     
    It should be clear what follows what by the placement on the page. With everything else being equal, a statement below another one executes after the first one. What makes things not equal is a control structure that disrupts the sequential flow of execution, such as a loop of some kind or a branch structure (if -else, switch).
    We'll help you with that:tongue:
     
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: Input/output using loops c++
  1. Input/Output Impedance (Replies: 15)

Loading...