hi
i have made a calculator code with if statement.
but i dont know why it doesnt work correctly.
it attentions on the first if task without including the condition.
so it only plus the first num to second num!
wether you put +,-,/,* it only add them to each other!
On lines 14, 17, 20, 23, 36, 39 42 and 45 you are using '=', which is for assigning a value to a variable, instead of checking for equality, which is '=='. Then, on lines 26 and 48, you are using incorrect statements. You must precede each check, with the variable you are checking. As in elseif (b != '/' && b != '+' && b != '-' && b != '*') The '&&' symbols means AND. So, this statement reads "If b does not equal '/' and b does not equal '+' and b does not equal '-' and b does not equal '*'', THEN do the following.
We can get to the rest of your request after getting what you have, working properly.
#include <iostream>
int main() // note: int main()
{
double first, second ; // favour double over float
char oper ;
// this loop continues as long as the user provides a valid input
// ie. as long as the attempted input is successful
while( std::cout << "\nenter <first number> [+-*/] <second number>\n"
<< "eg. 23.45 * 3.8 (enter 'q' to quit):\n? " &&
std::cin >> first >> oper >> second )
{
// the alternative is to use a switch-case construct
if( oper == '+' ) std::cout << first+second << '\n' ;
elseif( oper == '-' ) std::cout << first-second << '\n' ;
elseif( oper == '*' ) std::cout << first*second << '\n' ;
elseif( oper == '/' ) std::cout << first/second << '\n' ;
else std::cout << "invalid operation\n" ; // not one of +=*/
// for division first / second,
// see what would happen if second is zero?
// see what would happen if both first and second are zero?
}
}