Getline() Does Not Work If Used After Some Inputs

getline() does not work if used after some inputs

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);

getline not working properly ? What could be the reasons?

Since you have not posted any code. I am going to take a guess.

A common problem while using getline with cin is getline does not ignore leading whitespace characters.

If getline is used after cin >>, the getline() sees this newline character as leading whitespace, and it just stops reading any further.

How to resolve it?

Call cin.ignore() before calling getline()

Or

make a dummy call getline() to consume the trailing newline character from the cin >>

getline does not take any input

After cin >> SIZE;, the trailing newline is still kept in standard input.

You need cin.ignore() to skip the remaining newline character so the later getline() could get the value.

use of cin and getline are causing errors

You cannot just slap ignore() randomly and expect things to work. By default, ignore() will remove 1 character. If there is no character there, it will set eof() bit and your stream cannot read anything more until you clear() eof flag from it.

Only use ignore() when there is a leftover end of line (like after formatted extraction).

    void getdata(){
cout<<"Enter First Name"<<endl;
getline(cin,Firstname);

cout<<"Enter Last Name"<<endl;
getline(cin,Lastname);

cout<<"Enter your CNIC: "<<endl;
cin>>cnic;
cin.ignore();

cout<<"Enter your Address: "<<endl;
getline(cin,address);

cout<<"Enter your contact number: "<<endl;
cin>>contact;
cin.ignore(); // might want to clear up before next extractions

cout<<"Your Challan Number is: "<<challanno<<endl;
}

To use std::ws, you would need to use directly in getline calls

std::getline(std::cin >> std::ws, Firstname);

How to use getline when there is a previous input? getline(cin, stringName) not working because of previous imput

You don't need cin.clear(); or cin.sync();.
Use cin.ignore(); before getline.

getline and cin used one after another skips next inputs

What you need to do is cin.ignore() after a cin.

This is what happens:

When you use std::getline, it will get whatever is on that line, up until a newline character, but cin doesn't actually remove the newline character after performing a read, so your std::getline only gets an empty string.

get line function in c++ not working as it should

How @NathanOliver has suspected you used a cin. In your main method in lines 111 and 153 (if formated in the same way as above) you can find them. Try to change them to getline() too. That should work.



Related Topics



Leave a reply



Submit