# Thread: beginner calculator program problem

1. ## beginner calculator program problem

Hello! I am beginner trying to learn c++. Yesterday I wrote a simple calculator program, but when I run it I get strange results. Here is the code:

Code:
```#include <iostream>
using namespace std;
int main()
{
int br1;
int br2;
cout << "Enter first number: ";
cin >> br1;
cout << "Enter second number: ";
cin >> br2;
cout << endl << "Results: " << endl << endl;
cout << "First + second number = " << br1 + br2 << endl;
cout << "First - second number = " << br1 - br2 << endl;
cout << "First * second number = " << br1 * br2 << endl;
cout << "First / second number = " << br1 / br2 << endl;
cin.ignore();
cin.get();
}```
When I compile and run the program, I get this (Red color is what I enter):

Enter first number: 34593874
Enter second number: 34532224535
Results:
First + second number = -2112889775
First - second number = -2112889773
First * second number = -34593874
First / second number = 0
Process returned 0 (0x0) execution time : 8.596 s
Press any key to continue.

How could I, with adding and multiplying two positive numbers, get a negative number?

Should it not be that 34593874 + 34532224535 = 34566818409 and not -2112889775 ?

It is probably silly question, but I don't get it

PS. I used Code Blocks compiler.

2. Here's your program with the addition of a cout that prints the largest integer supported by the int type. Please compile, run, note that value's relation to what you're entering.

Code:
```#include <iostream>
#include <limits>
using namespace std;
int main()
{
int br1;
int br2;
cout << numeric_limits<int>::max() << endl;
cout << "Enter first number: ";
cin >> br1;
cout << "Enter second number: ";
cin >> br2;
cout << endl << "Results: " << endl << endl;
cout << "First + second number = " << br1 + br2 << endl;
cout << "First - second number = " << br1 - br2 << endl;
cout << "First * second number = " << br1 * br2 << endl;
cout << "First / second number = " << br1 / br2 << endl;
cin.ignore();
cin.get();

return 0;

}```

3. You have defined your variables as integers, yet this function has certain limitations whenever you type numbers that are whether too big or decimal...

Code:
```int br1;
int br2;```
Try defining them as double and you will get the results you're waiting for:

Code:
```
double br1;
double br2;```

4. Ok, I realize that integer data type has limitations, but I can't figure out what program actually did to produce that result ( First + second number = -2112889775 )?

5. It's called overflow and has to do with how hardware works.
If you add two 32-bit numbers, there is a chance that it may produce a result requiring 33 bits to represent. The hardware will then throw away the last bit, which will make your number appear "wrong."
Same goes for any other arithmetic operation, such as multiplication.

6. Thank you!