cin reading multiple words

Hi everyone,

I know this is asked a lot, i've looked it up and found several answers to my problem, but it just won't do. The code looks like this, i'll explain later what I want to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main(){
  string title;
  string year;
  string medium;
  cout << "Title? ";
  cin >> title;
  cout << "Year? ";
  cin >> year;
  cout << "Medium? (Bluray, Dvd, Vhs) "
  cin >> medium;
  cout << title << "(" << year <<", " << medium << ")" << endl;
  return 0;
}


Ok so this is pretty straightforward: I want the console to ask me some details about a movie, and then print it nicely. The problem is that this will only take one word for every input. While looking up I found something about getline(); this is where it gets tricky.

When I use getline(cin, title); (etc.) instead of cin << title; the console prints "Title? Year? " on one line and asks only for one input. Then it prints "medium? " and asks for a second input, then it prints the input. So suddenly I have only two inputs instead of three, I don't understand what's happening. In short, this doesn't work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main(){
  string title;
  string year;
  string medium;
  cout << "Title? ";
  getline(cin, title);
  cout << "Year? ";
  getline(cin, year);
  cout << "Medium? (Bluray, Dvd, Vhs) "
  getline(cin, medium);
  cout << title << "(" << year <<", " << medium << ")" << endl;
  return 0;
}


What did I do wrong? Thx for helping me out!
I don't know exactly what is going on , but it has to do something to do with carriage returns staying in the input buffer when reading a line...
Last edited on
Here, this explains the difference between formatted and unformatted input. Helped me differentiate the two a lot. :http://cplusplus.com/forum/general/69685/#msg372532
Thx for the answers, though this isn't really helping (:-p). I still don't have a clue why he skips the first input, so I don't hav any clue how to fix this.

edit: I just copy/pasted the code I wrote on this page into eclipse ... it seems to work on a different computer D; I love the magic in informatics ... !
Last edited on
you're missing a semicolon at line 14.
if it's still not working, try 2 getline statements for title.

1
2
3
cout << "Title? ";
getline(cin, title);
getline(cin, title);
Last edited on
When I ran it, it worked fine
except I had to add ; after Vhs) "



1
2
3
4
5
C:\Temp>testxxx3
Title? long name
Year? short name
Medium? (Bluray, Dvd, Vhs) dvd vhs
long name(short name, dvd vhs)
Last edited on
Well, like is explained in detail in the post I linked, the operator>> extracts input from the stream up to white space, but discards any leading white space. So if you use it more then once any white space that is left in stream is summarily discarded by the next >>. Unfortunately, this means if you are trying to get more then 1 word at the same time you have to use multiple >>. This is where getline comes in. BUT, getline does NOT discard leading white space. So you have to do it manually in the code with a black cin.getline() or something similar. There's also cin.ignore(numberofchars, sentinelchar). With ignore the numberofchars represents the maximum number of chars that ignore will read out of the stream and discard. sentinelchar is a char that, once ignore reaches it, will stop before it hits numberofchar, but which it will still extract and discard. So, for instance cin.ignore(1024, '/n') would extract either 1024 characters from the stream and discard them, or all characters up to and including a newline char, which would also be discarded.
Last edited on
Topic archived. No new replies allowed.