Modify the C++ program below so that it continues to divide two numbers until th
ID: 3693998 • Letter: M
Question
Modify the C++ program below so that it continues to divide two numbers until the user enters the character q (as a numerator or denominator) to terminate program execution.
#include
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
cout << " Enter a numerator (whole number only): ";
cin >> numerator;
cout << "enter a denominator (whole number only): ";
while (needDenominator)
{
cin >> denominator;
try
{
if (denominator == 0)
throw denominator;
}
catch (int e)
{
cout << " A denominator value of " << e << "is invalid." << endl;
cout << "Please reenter the denominator(whole number only): ";
continue;
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
needDenominator = false;
}
system("PAUSE");
return 0;
}
Explanation / Answer
Answer for Question:
Updated the code is given above with do while loop with choice of excution of programe.
See the below updated code..
#include<iostream>
using namespace std;
int main()
{
int numerator, denominator;
bool needDenominator = true;
char ch = 'a';
cout << " Enter a numerator (whole number only): ";
cin >> numerator;
cout << "enter a denominator (whole number only): ";
do
{
cin >> denominator;
try
{
if (denominator == 0)
throw denominator;
}
catch (int e)
{
cout << " A denominator value of " << e << "is invalid." << endl;
cout << "Please reenter the denominator(whole number only): ";
continue;
}
cout << numerator << '/' << denominator
<< " = " << double(numerator) / double(denominator) << endl;
needDenominator = false;
cout<<"you want quit the loop press q or Q or continue to press any key "<<endl;
cin>>ch;
}while(ch !='q' || ch!='Q');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.