PDA

View Full Version : Xcode stops running my program at cout




faithb
Apr 11, 2011, 11:55 PM
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;
}



gnasher729
Apr 12, 2011, 01:22 AM
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.

drsoong
Apr 12, 2011, 02:49 AM
"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.

jiminaus
Apr 12, 2011, 03:53 AM
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.

chrono1081
Apr 12, 2011, 09:32 AM
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.

balamw
Apr 12, 2011, 09:41 AM
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

chrono1081
Apr 12, 2011, 09:47 AM
OP a screen shot would help too, that way we can see which window you are typing in to.

ender land
Apr 12, 2011, 10:27 AM
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.

Ganesha
Apr 13, 2011, 11:23 PM
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.

chrono1081
Apr 13, 2011, 11:45 PM
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.