# Input/output using loops c++

1. Sep 24, 2013

### MostlyHarmless

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;
float Total;
float TotalCredits = 0;
float TotalPoints = 0;

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$

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

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

}

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
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
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
-1

Last edited: Sep 24, 2013
2. Sep 24, 2013

### 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.

3. Sep 24, 2013

### MostlyHarmless

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

{

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

{

//Updating Totals
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 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? 4. Sep 24, 2013 ### rcgldr 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? 5. Sep 24, 2013 ### 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)
{
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;