Hey there !
Ok so i am learning from Stroustrup's book, Programming Principle and practice" ,2nd edition.
I have a problem with the / operator, to be exactly if i will write " / 8 0 ", instead of giving me " 8/0 = 0" it gives me "8/0=4"
Without any boring stuff, here is the demand (Chapter 3 Exercise 10 - from the book) :
10 Write a program that takes an operation followed by two operands and outputs the result. For
example:
+ 100 3.14
* 4 5
Read the operation into a string called operation and use an if-statement to figure out which
operation the user wants, for example, if (operation=="+"). Read the operands into variables
of type double. Implement this for operations called +, –, *, /, plus, minus, mul, and div with
their obvious meanings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
using namespace std;
int main()
{
string operation;
double val1,val2;
cout<<"Please enter an operation(+,-,*,/,plus,minus,mul,div) followed by two floating-point values separated by a space: ";
while(cin>>operation>>val1>>val2) {
double res;
if(operation == "plus" || operation=="+") res=val1+val2;
else if(operation == "minus"|| operation=="-") res=val1-val2;
else if(operation == "mul" || operation=="*") res=val1*val2;
else if(operation == "div" || operation=="/") {
if(val2==0)
{
cout<<"Error! Trying to divide by zero!";
}
else{
res=val1/val2;}
}
cout<<val1<<operation<<val2<<"=="<<res<<endl;
cout<<"please try again"; }
cout<<"exit cuz of bad input";
}
|
Sorry for the long post :-s