combination of getline and cin

Hello,

When I use cin to read a line (like a number or char), then my program skips the first getline() it sees. After that, it continues as normal.
I found a solution by adding a getline() that I don't use afterwards, but I can't believe that is the way to solve this...

Anyone who can help me?

I add the working code here, so you know what I mean (It only works if I add the string 'strange' and read a line with it, though I don't actually read the line)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
#include <string>
#include <sstream>
using namespace std;


int main ()
{
	string mystr;
	string strange;
        string name;
	int year;
	
	cout << "Enter name: ";
	getline (cin,name);
	cout << "Enter year: ";
	cin >> year;
        getline (cin, strange);
	cout << "Do you like this friend?";
	getline (cin,mystr);

	cout << "\nYou have entered the friend: \n";
	cout << name;
	cout << "\t(" << year << ") \n";
        cout << "Do you like this friend: " << mystr << "\n";
	return 0;
}
Last edited on
since "year" is an "int" variable, "cin>>year" extracts only a numeric value, leaving the end-of-line that follows in the input buffer.
you can better control input if you only do getlines with strings, and extract other types from them using stringstreams..., try substituting "cin>>year" by:
1
2
3
string yearstring;
getline(cin,yearstring);
stringstream(yearstring) >> year;
Last edited on
Wat about removing the unnecessary getline and typing in cin.ignore();
just after the cin >> year; ? it works,im just amazed why martinlb solves this in many lines of codes. If there is anything wrong with my method,please explain it martin
Nothing wrong with your code, hazda. It actually does what Nele asked.

What I suggested is a method to better control input: Performing extractions directly on cin (using "cin>>") can be problematic in terms of expected behavior - not only for the case Nele was pointing out, but also for example if the user enters a word when a number is expected, weird things happen...

By separating user input and extraction operations, that can be avoided - so I always recommend to use this instead of extracting directly from cin.
Thanks for the help!

Actually, the code I submitted was a simplified part of the code I was working on. In the real case, I want to read in a char.

So I suppose with getline, it should go something like:
1
2
3
4
char c;
string mystr;
getline(cin,mystr);
c=mystr[0];


Is that correct? I find it difficult knowing when to use a string or a vector of chars...

But I'm also wondering: if I would use
1
2
3
4
string myst;
int year;
getline (cin,myst);
stringstream(myst) >> year;


and the user would type some characters, instead of a number, won't there be an error as well, when trying to use stringstream?

Anyway, thanks a lot already for the help!
Last edited on
If you just want to grab a single character, then getch(); might be a better choice.
Topic archived. No new replies allowed.