The problem I’ve been having
<code>#include <iostream>
using namespace std;
int main()
{
cout << "Enter an operator followed by two operands.";
int val1 = 0;
int val2 = 0;
char oper = '+';
cin >> oper >> val1 >> val2;
if (oper == '+')
cout << val1+val2;
else if (oper == '-')
cout << val1-val2;
else if (oper == '*')
cout << val1*val2;
else if (oper == '/')
{
if (val2 == 0)
cout << "Division with zero is not possible.";
else
{
double answer = 0;
answer = val1/val2;
cout << answer;
}
}
}
</code>
<code>#include <iostream>
using namespace std;
int main()
{
cout << "Enter an operator followed by two operands.";
int val1 = 0;
int val2 = 0;
char oper = '+';
cin >> oper >> val1 >> val2;
if (oper == '+')
cout << val1+val2;
else if (oper == '-')
cout << val1-val2;
else if (oper == '*')
cout << val1*val2;
else if (oper == '/')
{
if (val2 == 0)
cout << "Division with zero is not possible.";
else
{
double answer = 0;
answer = val1/val2;
cout << answer;
}
}
}
</code>
#include <iostream>
using namespace std;
int main()
{
cout << "Enter an operator followed by two operands.";
int val1 = 0;
int val2 = 0;
char oper = '+';
cin >> oper >> val1 >> val2;
if (oper == '+')
cout << val1+val2;
else if (oper == '-')
cout << val1-val2;
else if (oper == '*')
cout << val1*val2;
else if (oper == '/')
{
if (val2 == 0)
cout << "Division with zero is not possible.";
else
{
double answer = 0;
answer = val1/val2;
cout << answer;
}
}
}
Initially I hadn’t defined answer
and just tried to output val1/val2
, but that didn’t work, so instead I tried defining a variable (float/double) for it but still it gave me an int as an output.
New contributor
Avantika Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.