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)
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:
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.