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
How to Safely Use Openmp with C++11
Reinterpret_Cast VS. C-Style Cast
Cancelling Boost Asio Deadline Timer Safely
How to Pass Std::String to a Dll
Logical And, Or: Is Left-To-Right Evaluation Guaranteed
How to Read Output from Cmd.Exe Using Createprocess() and Createpipe()
How to Implement a Natural Sort Algorithm in C++
Why Doesn't Left Bit Shift << Shift Beyond 31 for Long Int Datatype
What Is Going on with Bitwise Operators and Integer Promotion
Are Virtual Destructors Inherited
Remove an Array Element and Shift the Remaining Ones
What Is Data Alignment? Why and When Should I Be Worried When Typecasting Pointers in C
Compute Median of Values Stored in Vector - C++
Initial Capacity of Vector in C++
What Is the Maximum Length in Chars Needed to Represent Any Double Value
How to Add and Subtract 128 Bit Integers in C or C++ If My Compiler Does Not Support Them