The following code is inside a do while loop choice is suppo
The following code is inside a do while loop. choice is supposed to be an integer input by the user. However, when a character is put instead of an integer, the code loops infinitely. Putting break; or exit(); after the catch statements terminates the whole program, which is wrong. How can i fix this?
#include <iostream>
#include <vector>
#include <iomanip>
#include<stdlib.h>
#include <cctype>
#include \"MyGrades.h\"
using namespace std;
int main ()
{
int choice; // user input for menu selection
double quiz, program, test; // user input for grades
MyGrades a; // class declaration
cout << fixed << setprecision(2);
cout << \"Welcome to My Grades APP. \" << endl;
do {
cout << \"\ Main Menu\" << endl << endl;
cout << \" 1. Set A quiz Grade\" << endl
<< \" 2. Set A Programming Assignment Grade\" << endl
<< \" 3. Set A Test Grade\" << endl
<< \" 4. Show All quiz Grades\" << endl
<< \" 5. Show All Programming Assignment Grades\" << endl
<< \" 6. Show All Test Grades\" << endl
<< \" 7. Show Overall Grades\" << endl
<< \" 9. Exit The Program\" << endl << endl;
cout << \"Enter your choice ---> \";
cin >> choice;
try {
if (!isdigit(choice))
throw choice;
} catch (char choice) {
cout << \"Error *** Incorrect input - You entered a character\" << endl;
cout << \"Enter a Positive Integer\" << endl << endl;
}
switch(choice)
{
case 1: cout << \"\ Enter Quiz grade: \";
cin >> quiz;
a.setQuiz(quiz);
break;
case 2: cout << \"\ Enter A Programming Assignment grade: \";
cin >> program;
a.setProgram(program);
break;
case 3: cout << \"\ Enter A Test grade: \";
cin >> test;
a.setTest(test);
break;
case 4: cout << \"\ Show All Quiz grades with Average.\" << endl;
a.getQuiz();
break;
case 5: cout << \"\ Show All Programming Assignment grades with Average\" << endl;
a.getProgram();
break;
case 6: cout << \"\ Show all Test Grades with Average\" << endl;
a.getTest();
break;
case 7: cout << \"\ Show Overall Grades.\" << endl;
a.showAll();
break;
case 9:
break;
default:
cout << \"\ Invalid Choice\" << endl;
break;
}
} while (choice != 9);
cout << \"\ Implemented by Emily Leyendecker and Maxwell Yi\" << endl
<< \"February - 2017\" << endl << endl;
return 0;
}
Solution
Answer:
goto for keep the program alive even in case of a non int input and the 2nd one is the solution of infinite loop .Here is the fixed code :
#include<iostream>
#include<cstdlib>
 #include<fstream>
 #include<cmath>
 using namespace std;
 int main()
 {
    int a[30];
    int s;
    fstream file;
    int sum=0;
    int q=0;
    double averge;
    double st;
    int standard_deviation;
    int avg;
    int square;
    cout<<\"\\t How many person\'s tempurature you wanna save \";
    cin>>s;
    int temp=0;
    for(int i=0;i<s;i++)
    {
    a[i]=rand()%100;
      
    }
    cout<<\"\\t \\t YOUR DATA HAS been saved in a file check file for results...\"<<endl;
    file.open(\"temps.txt\",ios::out|ios::app|ios::in);
   
        for(int i=0;i<s;i++)
        {
            file<<a[i]<<endl;
            sum=sum+a[i];
            avg=sum/s;
           
           
            st=avg-a[i];
            int s=-1*(st);
            square=s*s;
            temp=temp+square;
            temp=temp/s;
            standard_deviation=sqrt(temp);
           
           
        }
        file<<\"Mean =\"<<avg<<endl;
    file<<\"standard deviation =\"<<standard_deviation<<endl;
    file.close();
   
 }




