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.
getline not asking for input?
You have to be careful when mixing operator>>
with getline
. The problem is, when you use operator>>
, the user enters their data, then presses the enter key, which puts a newline character into the input buffer. Since operator>>
is whitespace delimited, the newline character is not put into the variable, and it stays in the input buffer. Then, when you call getline
, a newline character is the only thing it's looking for. Since that's the first thing in the buffer, it finds what it's looking for right away, and never needs to prompt the user.
Fix:
If you're going to call getline
after you use operator>>
, call ignore in between, or do something else to get rid of that newline character, perhaps a dummy call to getline
.
Another option, and this is along the lines of what Martin was talking about, is to not use operator>>
at all, and only use getline
, then convert your strings to whatever datatype you need. This has a side effect of making your code more safe and robust. I would first write a function like this:
int getInt(std::istream & is)
{
std::string input;
std::getline(is,input);
// C++11 version
return stoi(input); // throws on failure
// C++98 version
/*
std::istringstream iss(input);
int i;
if (!(iss >> i)) {
// handle error somehow
}
return i;
*/
}
You can create a similar function for floats, doubles and other things. Then when you need in int, instead of this:
cin >> i;
You do this:
i = getInt(cin);
I am having trouble with getline() function in C++
The problem with getline()
is that you are trying to assign a string stream to an int
variable houseNum
, hence the error.
no matching function for call to 'getline(std::istream&, int&)'
Also, the line of code:
std::cin >> firstName;"\n";
Should give you:
warning: statement has no effect [-Wunused-value]
For the "\n"
Enable compiler warnings, they save you a lot of time.
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);
Can't seem to get cin.getline to work help please?
Generally speaking, std::getline
is the better choice, as it works on std::string
directly:
#include <string> // for std::string, std::getline()
#include <iostream> // for std::cin
// ...
std::string a;
std::getline( std::cin, a );
Wont wait for getline C++
Here is a simple way to use the getline()
function :
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string name;
int age;
cout << "How old are you ?" << endl;
cin >> age;
cin.ignore(); // for avoiding trouble with 'name' when we're gonne use getline()
cout << "What's your name ?" << endl;
getline(cin, name);
system("PAUSE");
}
Don't forget it, if you use cin
befor getline()
, you have to put the line :
cin.ignore();
So your code should looks like something like that :
int main()
{
vector<person> people;
bool flag = true;
int num_people = 0;
//char*filename="names.txt";
people = read_file();
while(flag)
{
int sel = 0;
string args;
cout << "1 - Sök del av personnamn" << "\n" << "2 - Sök städer" << "\n" << "3 - Avsluta" << endl;
cin >> sel;
cin.ignore()
if(sel == 1)
{
cout << "Mata in delen av personnamnet" << endl;
getline(cin, args);
print_result(find_in_names(people, args));
}
else if(sel == 2)
{
cout << "Mata in staden" << endl;
getline(cin, args);
args = lower_letters(args);
print_result(find_person_from_city(people, args));
}
else if(sel==3)
{
flag=false;
}
else
{
cout << "FEL, TRYCK OM!" << endl;
}
}
}
Here's some doc http://www.cplusplus.com/reference/istream/istream/ignore/, hope it'll help;
Related Topics
Static Constant String (Class Member)
Do You Use Null or 0 (Zero) For Pointers in C++
How to Write Iso C++ Standard Conformant Custom New and Delete Operators
C++ "Virtual" Keyword For Functions in Derived Classes. Is It Necessary
What Happens If You Call Erase() on a Map Element While Iterating from Begin to End
Evaluating Arithmetic Expressions from String in C++
What Is the Performance Overhead of Std::Function
Why Should One Not Derive from C++ Std String Class
When Does a Constexpr Function Get Evaluated At Compile Time
Return a 2D Array from a Function
Initializer Lists and Rhs of Operators
Strptime() Equivalent on Windows
Get Human Readable Ast from C++ Code
Conveniently Declaring Compile-Time Strings in C++