Using C++. Need to get the output to look exactly like the one below. CALCULATE.
ID: 3602588 • Letter: U
Question
Using C++. Need to get the output to look exactly like the one below.CALCULATE.CPP You will write a program that mimicks a simple calculator that can do adding, multiplication, or modulo of 2 integers. The program takes 3 arguments at the command line: an integer, a character, and another integer. The character can be one of only 3 types:"+"X, or %. The program then returns either the sum, the product, or the modulo of the 2 integers. The program should be able to verify that 1) the user has exactly 3 arguments. 2) the operator used is one of the 3 allowed operators and nothing else. 3) when using modulo, the second integer is not zero (otherwise, you would divide by zero)
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
string input[4];
int z;
cout<<"$ ./calculate ";
cin>>input[0]>>input[1]>>input[2]>>input[3]; //Taking the inputs.
cout<<endl;
int x=stoi(input[0]); //Converting string to integer.
int y=stoi(input[2]); //Converting string to integer.
int size= sizeof(input)/sizeof(input[0]); //Calculating the size of string array.
if(!input[3].empty())
{
cerr<<"Number of arguments is incorrect."; // printing error
exit(1);
}
if(input[1]=="+"||input[1]=="x"||input[1]=="%")
{ if(input[1]=="+") //sum
{
z=x+y;
cout<<"$ "<<z;
}
else if(input[1]=="x") //product
{
z=x*y;
cout<<"$ "<<z;
}
else if(input[1]=="%"&&input[2]!="0")
{
z=x%y; //modulo
cout<<"$ "<<z;
}
else
cerr<<"Cannot divide by zero."; //printing error
}
else
cerr<<"Bad operation choice."; //printing error
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.