Code for a Modified Calculator How Can I Get This Code To: a.) Put blank lines i
ID: 3680021 • Letter: C
Question
Code for a Modified Calculator
How Can I Get This Code To:
a.) Put blank lines in between runs of the program to make the output file easier to read
b.) Use sentinel-controlled repetition (while statement) to allow the user to enter more than one set of integers for calculations and comparisons.
c.) Change the output to the file to be in the order and manner of the following:
The sum of integer1 and integer2 is sum. (Include input values in output.)
The difference between .... and .... is .....
The product of .... and .... is ....
The quotient of .... and .... is .... with a remainder of ....
OR: ***ERROR: Cannot divide by 0!
.... is larger (or greater) than .... OR The numbers are equal.
integer1 is even/odd (whichever it is)
integer2 is even/odd
-----------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a,b;
int sum, difference;
float product; // Declare Variables
double c,d;
ofstream outResults;
outResults.open( "Vidaurri_calcResults.txt", ios::app); //Opening the text file
cout << "Input two numbers" << endl;
cin >> a >> b; // Input two integers
outResults << " Numbers you are using are a = " << a << " b = " << b <<endl;
outResults << "Sum = " << a + b << endl; // Calculates all operations
outResults << "Product = " << a * b << endl;
outResults << "Difference = " << a - b << endl;
if(b==0) //Checking for dividing by 0
{
outResults << "division by 0 is not possible";
}
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 << " a is equal to b"; //Results are being outputted to the text file
}
if((a%2)==0) //Checking whether a and b are either even or odd
outResults << " a is even";
else
outResults << " a is odd";
if(b%2==0)
outResults << " b is even";
else
outResults << " 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
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int a,b;
int sum, difference;
float product; // Declare Variables
double c,d;
ofstream outResults;
outResults.open( "Vidaurri_calcResults.txt", ios::app); //Opening the text file
cout << “ Input two numbers" << endl;
cin >> a >> b; // Input two integers
outResults << “ Numbers you are using are 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.