Question

In: Computer Science

The following code is inside a do while loop. choice is supposed to be an integer...

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 << "\nMain 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 << "\nEnter Quiz grade: ";

       cin >> quiz;

       a.setQuiz(quiz);

                   break;

               case 2: cout << "\nEnter A Programming Assignment grade: ";

       cin >> program;

       a.setProgram(program);

                   break;

               case 3: cout << "\nEnter A Test grade: ";

       cin >> test;

       a.setTest(test);

                   break;

               case 4: cout << "\nShow All Quiz grades with Average." << endl;

       a.getQuiz();

                   break;

               case 5: cout << "\nShow All Programming Assignment grades with Average" << endl;

       a.getProgram();

                   break;

               case 6: cout << "\nShow all Test Grades with Average" << endl;

       a.getTest();

           break;

               case 7: cout << "\nShow Overall Grades." << endl;

       a.showAll();

                   break;

               case 9:

                   break;

               default:

       cout << "\nInvalid Choice" << endl;

                   break;

           }

      

   } while (choice != 9);

   cout << "\nImplemented by Emily Leyendecker and Maxwell Yi" << endl

   << "February - 2017" << endl << endl;

   return 0;

}

Solutions

Expert 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();
  
}


Related Solutions

Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Java Programming - please read the description I need this description. Inside a do/ while loop...
Java Programming - please read the description I need this description. Inside a do/ while loop (while choice !='x') , create a method call that calls a switch menu with 3 cases for variable choice and X for exit application.(this is a user input) Each case calls other methods. One of those methods asks for user input float numbers to calculate addition. And the result return to another method that displays the result. Thanks for your help! Thanks for asking....
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Write a while loop that will let the user enter a series of integer values and...
Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user backwards. do Loop...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT