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

Danksalot

macrumors newbie
Original poster
Jul 10, 2010
4
0
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
 

Danksalot

macrumors newbie
Original poster
Jul 10, 2010
4
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
A specific problem with C++ streams, in Xcode debugger, Debug build.
Fix is to undefine Preprocessor Macros = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

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

Threads that discuss problem and fix:
https://forums.macrumors.com/threads/867839/
https://forums.macrumors.com/threads/863761/
https://forums.macrumors.com/threads/837748/
https://forums.macrumors.com/threads/796818/
https://forums.macrumors.com/threads/780440/
.. 867839 lists specific steps to take.
.. 796818 notes input problems.
 

Matthew Yohe

macrumors 68020
Oct 12, 2006
2,200
142
I should've just searched here for that problem. I was Googling for Espinosa's thread on the XCode mailing list.
 

Danksalot

macrumors newbie
Original poster
Jul 10, 2010
4
0
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
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?
 

Danksalot

macrumors newbie
Original poster
Jul 10, 2010
4
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.