the most basic calc dont work

I started teaching myself C++ today and I wrote following codes so as to make a basic calculator that does addition but its not functioning. everytime i build it and run it on codeblocks the value is wrong and random. Can anybody tell me whats wrong in this program?
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
  #include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int c;
    int d;
    int sum;
    
    cout << "Please enter a number \n";
    cin >> a;
    cout << "please enter another number.. \n";
    cin >> b;
    cout << "please enter another number.. \n";
    cin >> c;
    cout << "please enter another number.. \n";
    cin >> d;
    
    sum = a+b+c+d;

    cout << "here i present the sum of those numbers \n"
    cout << sum;

    return 0;
}
I do not see anything wrong with your code except that it seems it has a typo. You forgot to place a semicolon after statement

cout << "here i present the sum of those numbers \n"
Last edited on
What is your input? If it's floats, it is because you declared your variables as int instead of double
Can you give us an example of the inputs you're using, and the result you're getting?
it might matter on what compiler he is using cause doesn't code blocks have the option of using different compilers?

Try downloading microsoft visual studios express since it is free.

Then see if your code works there, also when making a c++ command line program that you tell it to be an empty project before you code.
closed account (Dy7SLyTq)
doesn't code blocks have the option of using different compilers?


not true. you can

edit: but most people commonly use mingw
edit 2: and once he fixes the semicolon issue, a program that small shouldnt make a difference what compiler compiles it.
Last edited on
it might matter on what compiler he is using cause doesn't code blocks have the option of using different compilers?

There's nothing in the posted code that makes any use of compiler extensions or implementation-specific behaviour. So assuming the OP is using a proper C++ compiler, there's no reason to think the choice of compiler will make a difference.

Until the OP gives us something more to go on, it's going to be difficult to diagnose. And s/he seems to have gone quiet.
Topic archived. No new replies allowed.