I understand that there are better ways to validate input than what I have written. I am not trying to fix my code. I just want to understand why the second cin statement is skipped when the first receives a string into a double variable.
Trying to understand what is happening:
userInput is expecting a double, but a string is entered instead. The string is not saved into the variable. No change is made to existing data within userInput. But, then the second cin statement is skipped.
Why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{
double inputDouble = 1.0;
int skippedVar;
std::cout << "User input: ";
std::cin >> inputDouble;
std::cout << "Saved in inputDouble: " << inputDouble;
// This statement is skipped.
std::cin >> skippedVar;
std::cout << std::endl;
}
your code works as expected (or I don't understand your question)
line 9 you are asking user to input number (floating point) then you display entered text on screen. I am guessing that because you don't use endl; to get to the next line or some message to user, you only get ability to type number straight away.
add to the code
std::cout << "I have typed: " << skippedVar << std::endl;
on the other hand if you type not numbers in to first cin then I expect you get a lot of rubbish and this is causing program to crash?