Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getline() does not work if used after some inputs [duplicate]

Tags:

c++

char

getline

Possible Duplicate:
Need help with getline()

getline() is not working, if I use it after some inputs, i.e.

#include<iostream> using namespace std;  main() { string date,time; char journal[23];   cout<<"Date:\t"; cin>>date; cout<<"Time:\t"; cin>>time;  cout<<"Journal Entry:\t"; cin.getline(journal,23);   cout<<endl; system("pause"); } 

where as if I use getline() on top of inputs, it does work i.e.

cout<<"Journal Entry:\t"; cin.getline(journal,23); cout<<"Date:\t"; cin>>date; cout<<"Time:\t"; cin>>time; 

What might be the reason?

like image 787
Muhammad Arslan Jamshaid Avatar asked Oct 02 '12 13:10

Muhammad Arslan Jamshaid


People also ask

Does Getline work with double?

cplusplus.com/reference/istream/istream/getline getline doesn't take a double argument, as it's telling you. it's there to get lines of data (aka char arrays / strings) not numbers.

Why Getline is not working after CIN?

The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage.

Can we use Getline for multiple times in a C++ program?

You can just use the static method tinyConsole::getLine() in replace of your getline and stream calls, and you can use it as many times as you'd like.

Does Getline wait for input?

std::cin leaves the newline character in the buffer after pressing enter, and getline just grabs it and keeps going, that's why getline doesn't block to wait for input. cin>> will leave newlines in the buffer when the user presses enter. getline() reads this as the user having pressed enter to "skip" the input.


2 Answers

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delimiter if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of the file is reached in the input sequence or if an error occurs during the input operation.

When cin.getline() reads from the input, there is a newline character left in the input stream, so it doesn't read your c-string. Use cin.ignore() before calling getline().

cout<<"Journal Entry:\t"; cin.ignore(); cin.getline(journal,23); 
like image 152
P.P Avatar answered Sep 22 '22 23:09

P.P


Adding to what @DavidHammen said:

The extraction operations leave the trailing '\n' character in the stream. On the other hand, istream::getline() discards it. So when you call getline after an extraction operator, '\n' is the first character it encounters and it stops reading right there.

Put this after before getline call extraction:

cin.ignore()

A more robust way of taking input would be something like this:

while (true) {      cout<<"Time:\t";     if (cin>>time) {         cin.ignore();  // discard the trailing '\n'         break;     } else {         // ignore everything or to the first '\n', whichever comes first         cin.ignore(numeric_limits<streamsize>::max(), '\n');         cin.clear();  // clear the error flags         cout << "Invalid input, try again.\n";     } } 
like image 29
jrok Avatar answered Sep 21 '22 23:09

jrok