Xcode stops running my program at cout

Discussion in 'Mac Programming' started by faithb, Apr 11, 2011.

  1. macrumors newbie

    Joined:
    Apr 11, 2011
    #1
    Hi,

    I'm very new to Xcode and I'm just learning C++ . In a command line project, I'm having this issue where Xcode won't run my program past cout. I know the code is right, and i don't get any errors. When it asks me to enter a value, I do, and nothing happens. also, the program won't stop until I stop it. What should I do? I would really appreciate any help, because I haven't been able to find the answer online or in the xcode help!

    here's the code

    #include <iostream>

    int main()
    {
    using namespace std;
    cout << "Enter a number: ";
    int nValue;
    cin >> nValue;

    if (nValue > 10 && nValue < 20)
    cout << "Your value is between 10 and 20" << endl;
    else
    cout << "You value is not between 10 and 20" << endl;
    return 0;
    }
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    "I know the code is right": Don't be stupid. You never know that your code is right. You are using the same tools that Apple uses to produce MacOS X. So what is more likely: These tools are so broken that your dinky little program doesn't work, you are doing something stupid, or your code is wrong?

    I suggest you first figure out how to use the debugger and step through your code line by line, observing how variable values change as you step through the code, and that you figure out how to display the console.
     
  3. macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #3

    Apart from the fact that I in principle support the primary reasoning of your argument. No code is correct, until verified to be correct through testing.

    But his code does compile. I do, however, as many other Macrumors members strongly recommend the OP to learn how to use gcc on the command line.

    g++ program.cc -o program

    does show that your code is apparantly doing what it should. But...

    ...that is very likely exactly what he is doing. I suppose you accidently set a breakpoint in Xcode, and your program stops there.

    So, disable the breakpoint by clicking on it.
     
  4. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    I would say mostly like the OP's trying type input into the Debugger console and it's not getting to his program for some reason.

    gnasher729's solution of compiling and running from the command-line is probably the most reliable.

    But faithb, what version of XCode are you using? The Debugger console changed quite a bit from XCode 3 to XCode 4.
     
  5. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #5
    It compiled for me in Netbeans (I know I know I have Netbeans....I also have XCode but haven't tried it in there).

    I didn't realize you could stick namespace std; inside the main() until I saw the OP's code. It makes sense I just never saw it before.
     
  6. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #6
    faithb: just some tips to help us help you.

    • Use CODE tags around code you post.
    • Be very specific about what you expect and what you got. There are many couts in your code. Which one does it stop working on?
    • Tell us as much as you can. Version of Xcode, which window you are looking at, etc...

    Read these two guides for more tips http://whathaveyoutried.com/ http://mikeash.com/getting_answers.html

    B
     
  7. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #7
    OP a screen shot would help too, that way we can see which window you are typing in to.
     
  8. macrumors 6502a

    Joined:
    Oct 26, 2010
    #8
    try moving


    outside your main function. I have no idea what effect this has and it's not really the best practice to randomly include that inside some function.

    Also, if you know your code is right, why isn't it working? You can get all sorts of errors runtime even in programs that compile fine.

    If that isn't the cause I'd try looking at your code to verify you aren't hitting breakpoints (like others said) or trying to type in the wrong window.
     
  9. macrumors regular

    Joined:
    Aug 4, 2009
    #9
    Inside the the debug window the enter key and return key are not the same. Try pressing the return key. cin will not process the input until you hit the return key.
     
  10. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #10
    AH I forgot about that. That may be the issue because I ran into that long ago when I first got my Mac and tried out XCode.
     

Share This Page