My First C++ Program (Simple Calculator)

I am just beginning C++ coding. Here is my first complete program. Please mention if any mistake. Thanks.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 #include <iostream>

using namespace std;

int main()
{
    int a, b, resultA, resultB, resultC, resultD, answer;

    cout << "Please Enter a Number: ";
    cin >> a;
    cout << "Please Enter another Number: ";
    cin >> b;

    cout << "Enter Your Choice: (1 = Sum, 2= Sub, 3= Mul, 4= Div)" << endl;
    cin >> answer;

    resultA = a + b;
    resultB = a - b;
    resultC = a * b;
    resultD = a / b;


    if (answer == 1)
    {
       cout << "Your Answer is: " << resultA << endl;
    }

    else if (answer == 2)
    {
       cout << "Your Answer is: " << resultB << endl;
    }

    else if (answer == 3)
    {
       cout << "Your Answer is: " << resultC << endl;
    }

    else if (answer == 4)
    {
       cout << "Your Answer is: " << resultD << endl;
    }

    else
    {
       cout << "Your Choice is Incorrect." << endl << endl;
    }

    return 0;
}
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?
Last edited on
Well, thanks a lot, i appreciate your interest to guide me. In fact i am absolutely new in coding, so will be a bit difficult yet for me to work with switch etc. Any how , will try to understand. Thanks.
And what do you mean by "UB"?
Last edited on
Undefined Behavior. The code constructs which behavior is not defined by Standard. They might seem to work, until you compile your code again, slightly change it or just after some time. What more, it mere existence can change behavior of unrelated parts of code

Beginners guide to Undefined behavior: http://www.cplusplus.com/faq/beginners/undefined/

An article on how undefined behavior might change your whole program by Raymond Chen, Microsoft Principal Software Design Engineer: http://blogs.msdn.com/b/oldnewthing/archive/2014/06/27/10537746.aspx

Guide to undefined behavior by Chris Lattner, creator of LLVM and clang in particular: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
This program is good for a practice or trivial program. However, in production code, it would not suffice. Try using std::stringstream to test for invalid tokens. Also, restructuring your code as stated above would greatly improve readability and maintainability.
@suzuka what MiiNiPaa says about "using namespace std;" is true if you're inside a .h or .hpp file, but you're very obviously in a .cpp file. Go ahead and use "using namespace std;" if you'd like, but also as MiiNiPaa said please go look up why it's "bad".

@MiiNiPaa I refuse to have a function in my program that explicitly says I return an integer and not have a return statement in it. It's not good for beginners to believe that this is acceptable practice. Did you know that you can return multiple types in c++ if you just throw exceptions? Doesn't make it a good idea though :P
Go ahead and use "using namespace std;" if you'd like
Not before you know how it works and all implications. It is a bad practice for a reason. Please, read article I linked. Rescently I had to fix problem which would break code as soon as C++17 roll out (using namespace boost; + any).

1
2
using namespace std;
using namespace boost;
 are dumping too much in global namespace. I would say, do not use using-directive unless you know all names it will bring you.

It's not good for beginners to believe that this is acceptable practice.
It is an acceptable practice in the world, and it will help to remember that main is a special function with has mandated signature, cannot be overloaded, cannot be called by user code and has a special threatment in language.

Did you know that you can return multiple types in c++ if you just throw exceptions?
throwing exception is not returning. Variable waiting for funtion result will never get it, different code path would be chosen. Also "not yest is standard" library has types for returning different types already.
Hi,

Just two more really small things to add to all the great advice so far:

- Remember to always check for division by zero.

- Always initialise your variables with something, note that zero is not always a good choice. This a sort of golden rule - lots of people are caught out by not doing this - and they are not always beginners either (speaking from the perspective of seeing what people have done over the years). I like to do this regardless - even when input is read into that variable on the next line. It could be seen as a defensive thing that might save oneself one day. It might also be considered a personal preference.

Anyway, I hope that these small things might be of help.
Wow, got many precious words from all of you and will keep them in mind. Thanks.
Topic archived. No new replies allowed.