Looking at the tutorial, and trying out the code. Found that whenever I try using the:
getline (cin, mystring);
The line gets ignored and it just skips to the next line(s). There is no error, or warning either and I have included all the same headers. The compiler I'm using is the GNU GCC Compiler with the Code::Blocks IDE.
That's not really a proper way to fix your issue. cin.get() grabs the next character out of the buffer. Let's say the user typed 10 characters when you only needed one, the getline will be ignored again and just grab the junk that's left in the buffer. That is why ignore is typically the better way to go.
The way to avoid this altogether is to just get all input using std::getline(). std::getline() never leaves any characters on the input buffer, so you'll never run into this problem.
But then you either have to hope the user presses enter after each input or parse each string that is entered. This can be a real hassle. (I know you can change the delimiter, but then you're just going right back to the same issues as cin).
std::cin can accept multiple inputs from one line, it doesn't require the user to press enter to go to the next line. A good example of this is demonstrated below:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main() {
std::string firstName, lastName;
std::cout << "Please enter your first and last name: ";
std::cin >> firstName >> lastName;
std::cout << "Your name is: " << firstName << " " << lastName;
return 0;
}
Please enter your first and last name: Volatile Pulse
Your name is: Volatile Pulse
Using getline:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
int main() {
std::string firstName, lastName;
std::cout << "Please enter your first and last name: ";
std::cin.getline(firstName);
std::cin.getline(lastName);
std::cout << "Your name is: " << firstName << " " << lastName;
return 0;
}
Please enter your first and last name: Volatile Pulse
Pulse
Your name is: Volatile Pulse Pulse
The issue with getline is that it forces the user to press enter to enter input into another variable, as I mentioned before. Switching the delimiter has the same issue as cin. There is no 100% way to do it (at least not that I'm aware of).
You're 100% right that parsing is the best way to do it, especially if you're trying to get a specific input from the user. But making things overly complicated isn't always necessary for a beginner (and hoping you don't have a complete idiot or a masochist using your program).
But there is a lot of things that should be done, and I know there is code that even some of the better members here have written that aren't 100% perfect (error testing, etc.) but for simple/basic applications, there is nothing wrong with taking the shortcuts.
I do, however, believe that everyone should learn how to properly parse strings. Not necessarily just for input in this kind of situation, but I noticed another thread where the user used something similar to this but used cin's and you had to tell the program which kind of data you're entering so that the code wouldn't break. If they would have parsed the string from the beginning, they would have made it a lot easier on themselves and the end user as well.