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; }
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....
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:...
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...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT