Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top