Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to repeat an input command if input is invalid in c++
    primarykey
    data
    text
    <p>I have to write a program that calculates GPA with several user inputs.</p> <p>I've gotten the program to correctly calculate the GPA if the inputs are correct, however, the program must also have error checking i.e. if the user inputs 5 when the inputs actually needs to be 0,1,2,3, or 4. I need to be able to tell the user that the input is invalid AND have the program go back a step and allow them to retry.</p> <p>The program cannot use arrays.</p> <pre><code>#include &lt;iostream&gt; 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&lt;&lt;"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:\n"; cin &gt;&gt; LetterGrade; // Logic to ensure valid letter grade input if ((LetterGrade &gt;= 4) &amp;&amp; (LetterGrade &lt;= 0)) { cout &lt;&lt; "Please enter a valid input (0, 1, 2, 3, or 4):\n"; cin &gt;&gt; LetterGrade; } cout &lt;&lt; "Please enter the credit hours for the previously entered grade: \n"; cin &gt;&gt; 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 &lt;&lt; "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:\n"; cin &gt;&gt; LetterGrade; if (LetterGrade != -1) { cout &lt;&lt; "Please enter the credit hours for the previously entered grade: \n"; cin &gt;&gt; CreditHours; } }//close loop //Incomplete/Questionable if (TotalCredits &lt;= 0) { cout &lt;&lt; "Please be sure your Credit Hours add up to a positive, non-zero value\n"; } else if (TotalCredits &gt; 0) { //Calculating and printing the final GPA. float gpa = TotalPoints / TotalCredits; cout &lt;&lt; "Your GPA is:"&lt;&lt; gpa &lt;&lt;endl; } return 0; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload