int main()
{
int radie = -1;
while (radie != 0)
{
cout << "Ange cirkelns radie (0 avslutar): ";
cin >> radie;
cout << radie << endl;
}
return 0;
}
Limiting input type in C++
I want to limit the type of input the user makes in my short C++ program for my uni. Here is the code I have
I want to be able to limit the input that is stored in radie to numbers only. If I input a char or a string, the program loops endlessly. Please help Obi-Wan Kenobi.. you are my only hope :-D
add a cin.ignore(80,'\n'); or something like that (look up .ignore) after the cin >> statement.
There are a few things that could make it an infinite loop. One being a spare newline character in the input buffer (namely '\n'). To remove this you would do something of the form
It should be noted that the above does not work in all situations, but there is no completly portable way to flush an input streams buffer(s). Secondly you can get problems if the stream enters a failed state. This might happen if you enter a string instead of a number into your integer. Doing this would set the streams fail flags. When a stream is in a failed state, all subsequent reads from the stream will fail, causing an infinite loop in this case. You should check for this condition each loop iteration.
You could combine these together into a single function, or stream manipulator.
std::cin.ignore(std::cin.rdbuf()->in_avail(), '\n');
It should be noted that the above does not work in all situations, but there is no completly portable way to flush an input streams buffer(s). Secondly you can get problems if the stream enters a failed state. This might happen if you enter a string instead of a number into your integer. Doing this would set the streams fail flags. When a stream is in a failed state, all subsequent reads from the stream will fail, causing an infinite loop in this case. You should check for this condition each loop iteration.
if(std::cin.fail()){std::cin.clear();}
You could combine these together into a single function, or stream manipulator.
void inputstreamcheck(){ if(std::cin.fail()) { std::cin.clear(); } std::cin.ignore(std::cin.rdbuf()->in_avail(), '\n');}int main(){ int radie = -1; while (radie != 0) { std::cout << "Ange cirkelns radie (0 avslutar): "; std::cin >> radie; inputstreamcheck(); std::cout << radie << std::endl; } return 0;}
#include <iostream>#include <limits>using namespace std;int main(){ int radie = -1; while (radie != 0) { cout << "Ange cirkelns radie (0 avslutar): "; cin >> radie; if(cin.fail()) { // Clear the fail flag cin.clear(); // Discard the rest of the line cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } else { cout << radie << endl; } } return 0;}
edit: cin.ignore() accepts std::numeric_limits<int>::max() std::numeric_limits<std::streamsize>::max() as a magic number to mean "any number". And don't forget to cin.clear() the fail flag.
[Edited by - Fruny on January 18, 2005 2:14:12 PM]
Quote:Original post by RigidBody
Technically that would be std::numeric_limits<std::streamsize>::max() Fruny. [wink]
Correct. Rating++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement