-2

http://pastebin.com/mPp4z3LZ

Above is the link to my code where I was supposed to use class in order to create online rental store. During the process I was supposed to read the name of the movie and actors. That's why I decided to use getline function which was supposed to help me read more than one words. But my program would just skip the getline part and would not let me enter the name of movies or actors.

I found out after reading a post similar to this one that has been posted earlier in stack overflow that getline function would not work when you mix it with cin>>.As a matter of fact when I only use getline function to input data into my variables the program would run fine. I would therefore like to know if there is any way to use both the cin and getline function in a program.

Ufomammut
  • 178
  • 1
  • 12

1 Answers1

-1

After formatted input (cin.operator>>), a newline character can get left in the stream. When getline is called, it reads that newline character and stops there. You can use ignore whenever you jump between operator>> and getline.

std::cin >> some_var;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, some_string);

That call to ignore will discard everything up to and including the next newline character.

Weak to Enuma Elish
  • 4,531
  • 3
  • 22
  • 36