singh
singh

Reputation: 449

why does cin works wheras cin.getline doesn't?

My code is

char buffer[10]={0};
cin>>buffer; //here i enter the contents as "12345678912345"
//Now i tried to enter 14 characters into a buffer of 10
cin>>buffer; //This works as it waits for input on the console
cout<<buffer;

wheras

char buffer[10]={0};
cin>>buffer;//same input as above "12345678912345"
cin.getline(buffer,10);//This doesn't wait for input on console and returns 
cout<<buffer;

why does this happen?

THANKS: thanks everyone for your answers they were helpful

Upvotes: 2

Views: 1088

Answers (4)

perreal
perreal

Reputation: 98108

The >> operator does not consume any whitespace including the newline. The getline function returns when it sees a newline. Your problem is the newline that >> statement leaves at the buffer. You can solve this by consuming all characters, including the newline until you see a newline in the buffer. See the ignore function:

char buffer[10]={0};
cin>>buffer;
cin.ignore('\n', 256);
cin.getline(buffer,9); 
cout<<buffer;
return 0;

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154025

First off you have a buffer overrun: Reading to a char* stops when the stream goes bad, a space is found, or the width() is exhausted if it were bigger than 0. You want to use

std::cin >> std::setw(10) >> buffer;

Assuming this undefined behavior is abouded, the difference between using formatted and unformatted input for the second read is that formatted input starts with skipping leading whitespace whike unformatted input does not. Your first input stopped right before the newline character resulting from the enter key. That newline is good enough for getline() but it is skipped when using >>.

When switching between formatted and unformatted I/O you normally want to skip leading whitespace using, e.g., the std::ws manipulator:

(std::cin >> std::ws). getline(buffer, 10);

You should also consider using std::string together with std::getline().

Upvotes: 3

Mats Petersson
Mats Petersson

Reputation: 129524

Neither of your code-snippets "work" in the sense that both overflow the buffer in the first input line. This is not a good thing at all.

The second case doesn't wait for input because of technical differences between operator>> and getline - they react differently to the newline character that you have entered. When reading the first cin >> buffer, the newline at the end of the input is left in the input stream (cin). In your first case, the second cin >> buffer will skip over the newline that is in the input buffer, and then wait for more input.

In the second case, because of the workings of getline, it accepts the remaining newline as input, and buffer is not filled in with anything.

This is a common problem when mixing getline and operator>> for input. The best solution to solve that particular problem is to either use only operator >> or only getline - there are various other solutions, but they are generally quite tricky to get just right.

Upvotes: 4

Abhishek Bansal
Abhishek Bansal

Reputation: 12705

When you input the value for buffer in console, the return is still stored in the input stream.

Hence the termination character '\n' is already read by getline() from the previous read operation. This is why this function does not wait further for user input.

Try reading the getline() before the cin >> operation.

Also, as noted by Mike, you should be careful not to cause buffer overflow.

Upvotes: 2

Related Questions