How Can I Get This Code To: a.) Use sentinel-controlled repetition (while statem
ID: 3680210 • Letter: H
Question
How Can I Get This Code To:
a.) Use sentinel-controlled repetition (while statements) to allow the user to enter more than one set integers for calculations and comparisons.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int a,b;
int sum, difference;
float product; // Declare Variables
double c,d;
ofstream outResults;
outResults.open( "Vidaurri_calcResults3.txt", ios::app); //Opening the text file
cout << " Input two numbers" << endl;
cin >> a >> b; // Input two integers
outResults << " Numbers you are selecting a = " << a << " b = " << b <<endl;
outResults << " The Sum of integer" << a << "and integer" << b << "is" << a+b << endl;
outResults << "The Product of " << a << "and" << b << "is" << a * b << endl;
outResults << "The Difference between" << a << "and" << b << "is" << a - b << ends;
if(b==0) //Checking for dividing by 0
{
outResults << "Cannot division by 0!";
}
else{
outResults << "Quotient = " << a / b << endl;
outResults << "Remainder is " << a % b << endl;
}
if(a>b) //Checking if a is greater than, less than, or equal to b
{outResults << " a is greater than b";
}
else
{outResults << " b is greater than a";
}
if(a==b)
{
outResults << " The numbers are equal "; //Results are being outputted to the text file
}
if((a%2)==0) //Checking whether a and b are either even or odd
outResults << " integer a is even";
else
outResults << " a is odd";
if(b%2==0)
outResults << " Integer b is even";
else
outResults << " integer b is odd";
outResults.close(); //Closing the text file
cout << "Enter two decimal numbers" << endl;
cin >> c >> d; // Input two decimal numbers
cout << "Sum = " << c + d << endl; // Calculates all operations
cout << "Product = " << c * d << endl;
cout << "Difference = " << c - d << endl;
cout << "Quotient = " << c / d << endl;
cout << "Remainder is " << static_cast<int> (c) % static_cast<int> (d) << endl;
// Used static cast to change c and d to int and calculate remainder
system("pause");
return 0;
}
Explanation / Answer
Modified code is given below:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int a,b;
int sum, difference;
float product; // Declare Variables
double c,d;
ofstream outResults;
outResults.open( "Vidaurri_calcResults3.txt", ios::app); //Opening the text file
char wantToContinue;
do
{
cout << " Input two numbers" << endl;
cin >> a >> b; // Input two integers
outResults << " Numbers you are selecting a = " << a << " b = " << b <<endl;
outResults << " The Sum of integer" << a << "and integer" << b << "is" << a+b << endl;
outResults << "The Product of " << a << "and" << b << "is" << a * b << endl;
outResults << "The Difference between" << a << "and" << b << "is" << a - b << ends;
if(b==0) //Checking for dividing by 0
{
outResults << "Cannot division by 0!";
}
else{
outResults << "Quotient = " << a / b << endl;
outResults << "Remainder is " << a % b << endl;
}
if(a>b) //Checking if a is greater than, less than, or equal to b
{
outResults << " a is greater than b";
}
else
{
outResults << " b is greater than a";
}
if(a==b)
{
outResults << " The numbers are equal "; //Results are being outputted to the text file
}
if((a%2)==0) //Checking whether a and b are either even or odd
outResults << " integer a is even";
else
outResults << " a is odd";
if(b%2==0)
outResults << " Integer b is even";
else
outResults << " integer b is odd";
outResults.close(); //Closing the text file
cout << "Enter two decimal numbers" << endl;
cin >> c >> d; // Input two decimal numbers
cout << "Sum = " << c + d << endl; // Calculates all operations
cout << "Product = " << c * d << endl;
cout << "Difference = " << c - d << endl;
cout << "Quotient = " << c / d << endl;
cout << "Remainder is " << static_cast<int> (c) % static_cast<int> (d) << endl;
// Used static cast to change c and d to int and calculate remainder
cout<< "Do you want to continue again (Y/N)? ";
cin >> wantToContinue;
}
while(wantToContine == 'Y' || wantToContine == 'y' );
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.