1

I have a very strange problem. Here is my code:

#include <iostream>
#include <string>

using namespace std; 

int main(void)
{
    string user_name;
    cout << "what is your name?" << endl;
    getline(cin, user_name, '\n');
    cout << "hello, " << user_name << ", how are you today?" << endl;
} 

This code doesn't end when I push Enter at all; therefore there is no way to complete the input. The output will stop like this and wait:

what is your name?

However if I change '\n' to 'p' or whatever char, it will finish the input when the specific char is input. For instance:

#include <iostream>
#include <string>

using namespace std; 

int main(void)
{
    string user_name;
    cout << "what is your name?" << endl;
    getline(cin, user_name, '\p');
    cout << "hello, " << user_name << ", how are you today?" << endl;
} 

Screen:

what is your name?

hello, Frank, how are you today?

RUN SUCCESSFUL (total time: 2s)

PS: I am using NetBeans IDE 8.0 and Windows 8 Pro x64

Frank
  • 333
  • 3
  • 6
  • By the way, it's 'p' not '\p' but I cannot edit my own question. – Frank May 12 '14 at 21:23
  • getline(cin, user_name, '\n') is the default behavior for getline. Have you tried getline(cin, user_name);? what about cin >> user_name? – WhiteboardDev May 12 '14 at 21:29
  • It doesn't work without the third parameter either. I want to getline the string with spaces, so cin >> user_names is not working. – Frank May 14 '14 at 06:31

2 Answers2

0

New Line character depends on the operating system.

Reminder - \r\n or \n\r?

I would leave out the third parameter of the getline call all together. This will use getline's default behavior and terminate the read on the enter key.

EDIT: After reading you are still having problems, I decided to run some tests on your code.

First, to confirm that the default behavior works without specifying the third parameter to the getline method.

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
   while(true){
      string user_name;
      cout << "what is your name?" << endl;
      getline(cin, user_name);
      cout << "hello, " << user_name << ", how are you today?" << endl;
   }
}

The output:

what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?

Second, to test manually specifying the escape sequence '\n' i did this:

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
  while(true){
      string user_name;
      cout << "what is your name?" << endl;
      getline(cin, user_name, '\n');
      cout << "hello, " << user_name << ", how are you today?" << endl;
  }
}

And the output:

what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteBoardDev
hello, WhiteBoardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?

compiled on g++ w/osx so i didnt bother testing the '\r\n' case. This brings up a good point about cross platform compatibility. If you want your code to work in multiple environments try to not specify anything platform specific in your code (aka the EOL sequence) and leave out that third parameter entirely.

Community
  • 1
  • 1
WhiteboardDev
  • 819
  • 7
  • 10
  • Both \r\n and \n\r even cannot be complied, giving this error: hello.cpp:10:29: warning: multi-character character constant [-Wmultichar] getline(cin, user_name, '\r\n'); – Frank May 14 '14 at 06:36
  • But I just fount that if I use '\r' I will work if I push Enter then Space, i.e., two keys in this order, the code will work perfectly. Quite strange situation for me. Anyway, thank you very much. – Frank May 14 '14 at 06:38
  • Hi, @WhiteboardDev I found even I could finish the input, however, the next time I cin into something, there would be a space in front of the string. Like: getline(cin,str1,'\r'); getline(cin,str2,'\r'); in front of "str2" there will be a space. Do you know why my default Enter doesn't work? I'm using NetBeans IDE 8.0 under Windows 8 Pro X64. – Frank May 15 '14 at 21:00
  • Thanks a lot for your patience, but both your codes do not work on my laptop Windows 8 Pro and my desktop Windows 7 Enterprise. Did you use the NetBeans? – Frank May 18 '14 at 09:59
  • now I am using Visual studio 2012, your code works perfect. I think maybe the third default parameter is quite different with Visual studio. – Frank May 18 '14 at 21:03
  • I did not use an ide for this. I simply made a test.cpp file in a text editor and compiled it on the command line using g++. I did the to remove any inconsistency introduced by different IDE's – WhiteboardDev May 18 '14 at 23:16
0

Now I have solved my question with the help of WhiteboardDev. In my coding environment, I should use '\r' and during the input I need push Enter and Space, two keys in order to complete the it.

Community
  • 1
  • 1
Frank
  • 333
  • 3
  • 6