4

Using Xcode with c++ I'm trying to create a simple console application. However my usage of cout and cin does not seem to work as I intend it to.

I'm expecting:

Testing: 12
input was 12

edit: i've cut down the code test as much as I can:

#include <iostream>

int main(int argc, const char * argv[]) {
    // insert code here...
    int num;
    std::cout << "Testing: ";
    std::cin >> num;
    std::cout << "input was " << num << std::endl;

    return 0;
}

Sample output:

12
Testing: input was 12
Program ended with exit code: 0

Is there something I'm missing here?

Adam Jarvis
  • 183
  • 1
  • 13
  • 2
    While it's great you provided sample code, please take care that it is compilable, specially if you are having issues at run-time. Please take the time to read this page on providing a [MCVE]. – François Andrieux Mar 29 '17 at 19:21
  • Apologies. I'm see why it would be impossible for someone else to compile. However my issue is just with the cout and cin area that I attempted to highlight. Hopefully someone can see an issue there – Adam Jarvis Mar 29 '17 at 19:24
  • 1
    After fixing your compiler errors, I was not able to reproduce your observed behavior. What compiler, compiler version and compiler flags are you using? Normally, any call to `std::cin` should flush `std::cout`, but it doesn't seem to be happening here. Try adding `<< std::flush` after each `std::cout`. If this solves your problem, it looks like a library implementation bug to me. Though perhaps, in the process of fixing the compilation errors, I've made a change that avoids the problem you are describing. – François Andrieux Mar 29 '17 at 19:24
  • I suggest reducing your code to the minimal amount needed to reproduce the problem. This probably means just doing a bunch of cin and cout statements with local variables. There is no need for the GameCharacter class or any of the other functions that have nothing to do with your question. – Code-Apprentice Mar 29 '17 at 19:24
  • @FrançoisAndrieux i'm just using the built in tools within xcode 8. Nothing custom or special. In fact, I'm certain that it worked just fine a few days ago yet I have changed nothing – Adam Jarvis Mar 29 '17 at 19:26
  • @AdamJarvis Are you entering your inputs by hand? If you supply your inputs fast enough, it may appear in your terminal in the way you describe. – François Andrieux Mar 29 '17 at 19:29
  • @FrançoisAndrieux I updated my question with a minified example. That Is all the code in my editor in a new project. – Adam Jarvis Mar 29 '17 at 19:31
  • Do you run your application in a terminal emulator? Do you enter `12` by hand after the application has started? – n. m. could be an AI Mar 29 '17 at 19:33
  • @AdamJarvis Please try using `std::flush` like this `std::cout << "Testing: " << std::flush;` and report on what you observe. As per my first comment, it looks like your stream isn't flushed when using `std::cin` while it should. Please confirm that you are providing your input by hand, giving time for your terminal to display the "Testing: " prompt. – François Andrieux Mar 29 '17 at 19:34
  • @n.m. It is using the build tool and terminal within Xcode 8. Yes I enter the input by hand – Adam Jarvis Mar 29 '17 at 19:34
  • @FrançoisAndrieux Yes I am entering the input by hand inside of the terminal within Xcode 8. Adding `std::flush` changed nothing sadly – Adam Jarvis Mar 29 '17 at 19:35
  • I'm guessing that it's a problem with the Xcode 8 terminal then. Try running it from the command line instead. – interjay Mar 29 '17 at 19:36
  • @AdamJarvis I recommend adding the Xcode tag to your question, as the problem does not lie with your code. The code you provided should in fact produce the results you expect. This looks like a problem with your development environment or your terminal. – François Andrieux Mar 29 '17 at 19:37
  • As far as I know there's no real terminal window in Xcode. Whatever interactive window it provides may or may not behave like a terminal. Try running from the command line in a real terminal emulator. – n. m. could be an AI Mar 29 '17 at 19:38
  • @FrançoisAndrieux Thanks, I have added the tags as suggested. It's also worrying me that the code in the question supposedly uses 1.7 MB of memory... – Adam Jarvis Mar 29 '17 at 19:39
  • 1.7 MB is normal. C++ runtime is quite large. – n. m. could be an AI Mar 29 '17 at 19:40
  • @n.m. after compiling and running inside a native terminal, the code works fine and as intended. Very weird stuff in Xcode then, as this worked before... – Adam Jarvis Mar 29 '17 at 19:41

1 Answers1

1

Apparently is a specific problem with C++ streams, in Xcode debugger, Debug build.

Try this:
1. Project -> Edit Active Target ...
2. Search for "preprocessor" in Build
3. Delete the values:
Preprocessor Macros = _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1

I found a similar issue but for Xcode 3.2.1 and C++ string fails!

You can try also this workaround:
Paste these lines at the very beginning of your program (before any #include statements):

#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
#undef _GLIBCXX_DEBUG
#undef _GLIBCXX_DEBUG_PEDANTIC
Community
  • 1
  • 1
Rama
  • 3,222
  • 2
  • 11
  • 26