When I type in a letter instead of a number it just keeps on looping. Infinite loop.
That's because this causes the failbit to be set (you can check it with cin.fail()), which will cause all subsequent operations to fail as well. Call cin.clear() in order to reset the fail bit.
How do I prevent this in an easy way?
The standard C++ streams and "easy" are mutually exclusive.
When I answer the Continue with multiple char (like Continue Y/N: BLAHA) it asks the same questions five times. ???
This is because you're reading only a single character. Use ignore or getline to extract any characters that might follow.
Also, this is incorrect: while(km)
km is not initialized. Use for(;;)
That's because this causes the failbit to be set (you can check it with cin.fail())
Thank you! Let's see if I got this correct. Something like:
1 2 3 4
cout << "How long is your trip?" << endl;
cin >> km;
cin.fail();
cin.ignore();
Do I need to put something in between the ()?
Where can I find more information about how to use the
ignore
or
getline
and how to extract chars?
I do know there is a problem with the while(km), but I did not know any other way to make it work. How do I set up the for loop in a correct way? Any example?
I do know there is a problem with the while(km), but I did not know any other way to make it work. How do I set up the for loop in a correct way? Any example?
As shown in the modified code I posted. You want a pseudo-infinite loop, so you don't need a condition - while doesn't allow omitting the condition, but for does. Alternatively, you can use while(true).