Thread: Help! Calculator Not Working...What is wrong?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    6

    Question Help! Calculator Not Working...What is wrong?

    Nothing wrong with compling, all done, but the EXE will not work when I calculate it.
    Some One Know What is going on?
    Code:
    int main ()
    {
        cout << "welcome to My First Calc!";
        cout << "Enter The ID Of the Task.\n 1.Add\n2.Subtract\n3.Multiply\n4 Divide\n";
        int taskid;
        cin >> taskid; // taskid is the Task ID.
        cout << "Enter the first number.\n";
        int num1;// the First Number
        cin >> num1;
        cout << "Now the Second.\n";
        int num2;// the second number
        cin >> num2;
        int result; // defining the result variable earlier
        if (taskid == 1)
        {
             cout << "Results:";
             result = num1 + num2;
             cout << result;
             cout << "Bye!";
             cin.get();
        }
        else if (taskid == 2)
        {
             cout << "Results:";
             result = num1 - num2;
             cout << result;
             cout << "Bye!";
             cin.get();
        }
        else if (taskid == 3)
        {
            cout << "Results:";
            result = num1 * num2;
            cout << result;
            cout << "Bye!";
            cin.get();
        }
        else if (taskid == 4) 
        {
             cout << "Results:";
             result = num1 / num2;
             cout << result;
             cout << "Bye!";
             cin.get();
        }
        else
        {
            cout << "Error:UNknown Task ID";
            cout << "Bye!";
            cin.get();
        }
    }
    Testing Enviroment:
    Windows 7 64Bit
    IDE:
    Dev-C++ 7.4.2 (Portable)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems OK to me.
    Code:
    $ g++ foo.cpp
    $ ./a.out 
    welcome to My First Calc!Enter The ID Of the Task.
     1.Add
    2.Subtract
    3.Multiply
    4 Divide
    1
    Enter the first number.
    12
    Now the Second.
    22
    Results:34Bye!
    Perhaps the screen disappears as soon as you enter the last number....

    Your cin.get() only consumes the newline after the input of the 2nd number, it does not wait for you to press a key.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    How do I Solve this issue?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Run it from command line
    or
    Run it with Ctrl+F5 (if in VS)
    or
    read FAQ > Flush the input buffer - Cprogramming.com
    or use
    cin.ignore http://www.cplusplus.com/reference/i...stream/ignore/
    Last edited by vart; 08-07-2013 at 11:47 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    Quote Originally Posted by vart View Post
    Run it from command line
    or
    Run it with Ctrl+F5 (if in VS)
    or
    read FAQ > Flush the input buffer - Cprogramming.com
    or use
    cin.ignore istream::ignore - C++ Reference
    How do I use cin.ignore? I use Dev-C++ not VS and can you modify my code to let it work? I am a noob!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    cin.ignore is standard C++, so it will work everywhere, regardless of your IDE.
    Simply put
    std::cin.ignore(std::numeric_limits<std::streamsiz e>::max());
    Before a std::cin.get() to ignore everything in the input buffer.
    EDIT: Remove space between "std::streamsiz" and "e". Board is incorrectly placing it there for some reason.
    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.

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    Thanks very Much!

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    6
    Just cin.ignore() will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with the code(Calculator program)
    By rizwan in forum C Programming
    Replies: 10
    Last Post: 10-19-2011, 05:59 PM
  2. Text Calculator, first program, not working correctly
    By jacob199651 in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 04:41 PM
  3. What's wrong with my calculator?
    By andrewmin in forum C Programming
    Replies: 3
    Last Post: 09-08-2010, 02:30 PM
  4. Replies: 3
    Last Post: 03-16-2010, 02:29 PM
  5. A Simple Calculator Program Not Working
    By oobootsy1 in forum C++ Programming
    Replies: 9
    Last Post: 01-09-2004, 09:34 PM

Tags for this Thread