Well, there is no de-facto mistakes. It works, it does not contain UB, it conforms to standard.
Things you might want to consider:
1) You can replace chaing of else-if statements with switch.
2) Learn why
using namespace std; is considered bad practice in real projects. At least know what it does and how to write code without it.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
3) Consider getting rid of intermediate variables, strive for lazy evaluation. In your case you can replace
resultA with
a+b, etc.
4) Declare variables just before they can be useful.
5)
return 0 is not required in
main() in C++.
6) Do not overuse
std::endl;. It forces a stream flush which is costly. Standard streams are usually flushed when it is needed, so you would need to care about it only in specific cases later.
'\n'
is as long and does not forces flush. Also consider embedding standalone
'\n'
in preceding string literals.
A rewrite of your program with these considerations:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
int main()
{
int a, b;
std::cout << "Please Enter a Number: ";
std::cin >> a;
std::cout << "Please Enter another Number: ";
std::cin >> b;
int answer;
std::cout << "Enter Your Choice: (1 = Sum, 2= Sub, 3= Mul, 4= Div)\n";
std::cin >> answer;
switch(answer) {
case 1: std::cout << "Your Answer is: " << a + b << '\n'; break;
case 2: std::cout << "Your Answer is: " << a - b << '\n'; break;
case 3: std::cout << "Your Answer is: " << a * b << '\n'; break;
case 4: std::cout << "Your Answer is: " << a / b << '\n'; break;
default: std::cout << "Your Choice is Incorrect.\n\n";
}
}
|
You do know about integer division and intended to use it here, do you?