954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Having trouble with cin >> in xCode...

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:

#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

Danksalot
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I don't know the solutions to this.
(this is a friendly and discrete bump because I think it is an interesting issue)

Excizted
Posting Whiz
309 posts since Oct 2009
Reputation Points: 94
Solved Threads: 27
 

Repost your question here -- it is specifically for mac

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I posted my questions over there too. Thanks for the link. I'll leave this up until somebody has an idea what may be going on.

Am I right that when the RETURN key is hit, it should go to the "cout <<" statement?

Danksalot
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Yes it should. I recall Microsoft has a similar problem at one time with vc++ 6.0 compiler -- the problem was in a header file which they eventually fixed.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Turns out I was hitting the ENTER key, not the RETURN key. Something so simple. Thanks for thinking through this with me.

Danksalot

Danksalot
Newbie Poster
3 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You