Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

faithb

macrumors newbie
Original poster
Apr 11, 2011
1
0
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;
}
 
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;
}

"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.
 
"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?


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...

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,

...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.
 
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.
 
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.
 
try moving


using namespace std;

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.
 
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.
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.