how can i Write a small calculator that takes as input one of the four arithmetic operations, the two arguments to those operations, and then prints out the result?
just as simple as that, i don’t need a real calculator.
here is what i tried so far, but well it didn’t work:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x,y,result;
string arithmatic;
cout<<"enter the first number:";
cin>>x;
cout<<"enter the second number:";
cin>>y;
cout<<"use one of the four artimatic operations /, *, + or - :";
cin>>arithmatic;
if (arithmatic=="/" )
result=x/y;
cout<<"x / y ="<<result;
if (arithmatic == "*")
result=x*y;
cout<<"x * y ="<<result;
if (arithmatic == "+")
result = x + y;
cout<<"x+y ="<<result;
if (arithmatic == "-")
result = x-y;
cout<<"x-y ="<<result;
else
cout<<"what is this? i said use arithmatic operations!";
return 0;
}
i know there is a lot wrong with this program, i just started learning out, and this practice was in a book.
A couple issues:
ifstatements. This results instd::coutbeing called numerous times throughout the code.ifstatements that is terminated by a singleelse, useifelse ifelseinstead.