#include <iostream> using namespace std; int main () { float input1, input2; float result; char op; cout << "Input a number: "; cin >> input1; cout << "Input +,-,x, or /: "; cin >> op; if (op == 'x') { cout << "Input number to multiply by: "; cin >> input2; result=input1*input2; cout << "The result is "; cout << result; cout << "."; } else if (op == '+') { cout << "Input number to add by: "; cin >> input2; result=input1+input2; cout << "The result is "; cout << result; cout << "."; } else if (op == '-') { cout << "Input number to subtract by: "; cin >> input2; result=input1-input2; cout << "The result is "; cout << result; cout << "."; } else if (op == '/') { cout << "Input number to divide by: "; cin >> input2; result=input1/input2; cout << "The result is "; cout << result; cout << "."; } cin.get(); cin.get(); return 0; } |
switch(op)
instead of if-else-if ladder - it's much let's say ergonomic and faster than if-else-if construction.... It won't take long to adjust your code...system("pause");
:-) But still it's not the most efficient way to end your program. For more info check the forum or wait for the link :-)
double
s instead of float
s, the results would have a better precision.
|
|
|
|
cout << "The result is " << ( result = input1*input2 ) << '.';
Finally, I consider you as newcomer in c++ (no offense, I'm newb too), that's why +1 for NOT using system("pause"); :-) But still it's not the most efficient way to end your program. For more info check the forum or wait for the link |
|
|