0

Picking up C++ and having a go at it on OS X 10.9 using XCode 5.0.2 and using Alex Allain as reference.

The following code compiles just fine and outputs correctly

#include <iostream>
#include <string>

using namespace std;

int main()

{
std::string user_first_name = "test";

std::cout << user_first_name << "\n";

return 0;
}

When I add a getline function, code appears to compile but with no output.

#include <iostream>
#include <string>

using namespace std;

int main()

{
std::string user_first_name = "test";

std::getline( std::cin, user_first_name, '\n' );

std::cout << user_first_name << "\n";

return 0;
}

In fact debug navigator has memory filling up with bars (although actual memory use is fixed at 276 KB). Why am I getting stumped on such a simple thing/concept?

I did a bit of digging around and its quite likely this is related to a text encoding issue. I'm using defaults which is Unicode (UTF-8). Encoding is not something I'm familiar with, never something I had to deal with when learning on Windows. How do I get past this?

David G
  • 94,763
  • 41
  • 167
  • 253
  • 1
    When you write something followed by the enter key, there's nothing being output then either? – Some programmer dude Jan 02 '14 at 16:38
  • "appears to compile but with no output" --- [This does not seem to be the case](http://ideone.com/6M6CmF). – n. m. could be an AI Jan 02 '14 at 16:40
  • Well on my end the first example of code outputs "test". The second doesn't even show that. That's what I meant but doesn't appear to output anything. – aatroxed Jan 02 '14 at 17:17
  • The second program doesn't show anything, because it's waiting for input (your `std::getline` call). If you give it some input it will continue and show the output. – Some programmer dude Jan 02 '14 at 18:01
  • Wow, I feel like a boob...My mistake. I'm not used to the XCODE IDE coming from Windows and the console didn't appear to be working. Wow, a thousand apologies for bringing up such a stink. This feels like a "did you plug it in?" question. – aatroxed Jan 02 '14 at 20:55

1 Answers1

0

I can't comment regarding the use of XCode or OS X, but it was my understanding that std::cin always gives you a narrow (single-byte) character stream. In Windows (at least with Visual Studio), I think it works whether you compile for UTF8 (single-byte for all ASCII characters) or UTF16 (2-bytes for all ASCII characters). The runtime library presumably does the conversion for you as necessary.

I'm not sure what "filling up with bars" means, but maybe it's just that you're looking at uninitialized memory. If you think that it is an encoding issue, perhaps try using wstring/wcin instead of string/cin and see if that helps.

Aaron
  • 594
  • 2
  • 12