I am new to programming and I am trying to do a problem out of a book. The problem is relatively simple but I encountered something along the way that I would like an explanation to. In the program, it takes in two different numbers and a string. I set it up to use cin for the two numbers and then use getline to take in the string. However, if I make it ask for the numbers before the string, the program doesn't wait for input to the string and the program just ends. If i ask for the string and then the numbers, it works just fine. Is there some rule that says you cannot use cin before getline? Thank you in advance. I'll include the part of the code that I'm having trouble with.
The problem with mixing cin and getline, is that cin leaves in the newline character in the buffer generally.
This might not mean anything to you right now, but basically there is a character in the buffer that getline sees, which happens to the value that getline terminates with.
To fix this, you need to make a call to either cin.sync() or cin.ignore()