C++ - Having trouble with cin >> in xCode...

Discussion in 'Mac Programming' started by Danksalot, Jul 10, 2010.

  1. macrumors newbie

    Joined:
    Jul 10, 2010
    #1
    I am using xCode on a Mac to write some c++ code.

    Any time I use cin >> the program compiles just fine, but when I run it something happens that I didn't expect. I enter what I want for input and hit the RETURN key, and it goes to the next line, waiting for me to type something else. As far as I know, it should stop looking for input at that point, but it doesn't. Here's an example:


    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
        {
        int Num = 0;
        cout << "Enter a number: " << endl;
        cin >> Num;
        cout << "You entered: " << Num << endl;
        return 0;
        }
    I never get to the "You entered: " statement, because I can't get it to assign the dang input! Anybody who knows what's going on, please let me know. Here's what the program looks like running:

    Enter a number:
    5
    15
    2546


    x
    b
    5



    No matter what I enter, when I hit the RETURN key, it just goes to the next line and waits. Thanks in advance for any help!

    Danksalot
     
  2. macrumors 68020

    Joined:
    Oct 12, 2006
  3. thread starter macrumors newbie

    Joined:
    Jul 10, 2010
    #3
    I just checked and I'm running xCode 3.1.4 on Leopard.

    Looks like I'll have to get Snow Leopard to update any farther.
     
  4. macrumors 68020

    Joined:
    Oct 12, 2006
    #4
    That's the latest version for Leopard. You need to update to Snow Leopard.
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    A specific problem with C++ streams, in Xcode debugger, Debug build.
    Fix is to undefine Preprocessor Macros = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

    http://forums.macrumors.com/showpost.php?p=8590820
    .. the specific problem and fix.

    Threads that discuss problem and fix:
    http://forums.macrumors.com/showthread.php?t=867839
    http://forums.macrumors.com/showthread.php?t=863761
    http://forums.macrumors.com/showthread.php?t=837748
    http://forums.macrumors.com/showthread.php?t=796818
    http://forums.macrumors.com/showthread.php?t=780440
    .. 867839 lists specific steps to take.
    .. 796818 notes input problems.
     
  6. macrumors 68020

    Joined:
    Oct 12, 2006
    #6
    I should've just searched here for that problem. I was Googling for Espinosa's thread on the XCode mailing list.
     
  7. thread starter macrumors newbie

    Joined:
    Jul 10, 2010
    #7
    I had success locating those files, and I deleted them, and it still does not work. The program still runs exactly as I have described above. I also tried using #undef in my program to no avail.

    Is there any way that I deleted them in the Edit Active Target menu and it didn't actually take them out?

    Any other ideas?

    I really appreciate your time and expertise!

    Danksalot
     
  8. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    File > New Project
    Command Line Utility, C++ Tool

    Edit main.cpp
    I copy and paste your source code exactly.
    Save.

    "Debug" is active build configuration.
    Build > Build. It builds.

    Run > Run.
    It runs.
    It prompts "Enter a number:".
    I press 5.
    Press Return.
    It prints "You entered: 5"
    It exits.

    Run > Run.
    It runs.
    It prompts "Enter a number:".
    I press 5.
    Press Enter. <--- DIFFERENT
    Nothing.
    Press Enter again.
    Nothing.
    Key in 33.
    Press Return.
    It prints "You entered: 33"
    It exits.

    Xcode 3.1.4. OS 10.5.8. Mac mini with Core 2 Duo.


    Questions:
    Are you hitting Return or Enter?
    Are you running with Run or Debug?
    Which build configuration?
     
  9. thread starter macrumors newbie

    Joined:
    Jul 10, 2010
    #9
    I guess I thought I was using the RETURN key, but I was using the ENTER key every time. That solved my problem. Thanks a heap!

    Danksalot
     

Share This Page