Hello chocotaco,
What you need to understand is the difference between "std::cin >> aVariable" and "std::getline(std::cin, aString);".
"std::cin >> aVariable" is known as formatted input. I cover that shortly. The way it works is when entering something in from the keyboard what you type is put into the "
input buffer" until you press "Enter" when it is put into the variable.
Not everything you type will be put into the variable. The first leading white space, if any, is ignored. Then "std::cin" will take from the "input buffer" up to but not including the first white space or new line it finds whichever comes first also extracting the white space or new line which it throws away and does not use. Leaving whatever may be left in the "input buffer".
As long as you follow the first "std::cin" with another "std::cin" it works.
To the formatted input part I would say it works best when the variable is defined as a numeric variable. For "std::cin >> NumericVariable" The "cin" knows to expect a number for input. Therefor if you should type something other than a number, say a letter like 'a', it causes "cin" to fail. Which is something you have to deal with. The following code can catch this:
1 2 3 4 5 6 7 8 9 10 11 12
|
std::cin >> numericVariable;
While (!std::cin)
{
std::cout << “\n Invalid Input. Anything else you might want to say.” << std::endl; // <--- Change message as needed.
std::cin.clear(); // <--- Clears the state bits on “cin”.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << “\n Your prompt” <<std:;endl;
std::cin >> numericVariable;
}
|
This will stay in the while loop until you enter a valid number. Which is not to say that the number will be what you want. You will have to add code to validate the number.
The "ignore" function takes two parameters. The first is a number usually something large like 1,000 or 10,000 it could even be 100,000. The bit of code
std::numeric_limits<std::streamsize>::max()
is just a way of getting the largest number available for your computer and compiler. It comes from the header file "limits". The second parameter is the delimiter. Normally the new line character ('\n'), but can be changed to something else if needed because the "ignore" function can be used for more than clearing the "input buffer".
The
std::getline(std::cin, aString);
works a bit different. First is that it only works with a "std::string". It will take from the "input buffer" everything entered from the keyboard including the new line character at the end and then it discards the new line character leaving the "input buffer" empty.
"getline" takes three parameters. The first is where the input comes from. This could be “std::cin” or a file stream for reading from a file. The second parameter is the string it will put the information in to and the third parameter is the delimiter. The delimiter has a default value of the new line ('\n') and is why you do not see it all the time.
The problem that most new people have is when they mix “std::cin >> aVariable” and "std::getline(…)" in the same program. What happens is that the "cin" will leave at least the new line character in the "input buffer" and when you get to the "getline" it finds something in the "input buffer" and does not wait, but puts what is there into the variable and moves on.
My best advice is to follow the last "std::cin >> aVariable;" with
std::numeric_limits<std::streamsize>::max(),'\n');
before you use "std::getline(…);". Sometimes it is easier to put this line of code just before the "std::getline" if you are not sure where the last "std::cin" is.
Note: using
std::cin.ignore();
with no parameter is OK, but the defaults for the parameters are one and new line. Ignoring only one character may work some time, but not all the time. It is better to use
std::numeric_limits<std::streamsize>::max(), '\n');
Hope that helps,
Andy