Thread: beginner calculator program problem

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    13

    Question 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. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    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;
    Last edited by Khabz; 03-02-2013 at 03:45 PM.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    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. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    13
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Calculator: Problem with expressions
    By SpudCake in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2011, 05:36 AM
  2. help with simple calculator (beginner)
    By philthemn in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:05 AM
  3. Problem in Calculator Program
    By CodeJerk in forum Windows Programming
    Replies: 1
    Last Post: 06-07-2003, 12:00 PM
  4. A calculator program problem?
    By correlcj in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2002, 03:54 PM
  5. C++ Beginner Simple Calculator
    By mom in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:51 AM