trying to prevent a char input
I'm trying to prevent an accidental char input but all my code does is repeat the error message.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
string input = "";
stringstream myStream;
int hop;
while (true)
{
stringstream myStream(input);
cout << "What is the transmition range?" << endl;
cin >> hop;
if (myStream >> input)
break;
cout << "Invalid entery, ";
}
|
I think what you're trying to do is if (cin >>hop)
.
no that did not stop the repeating
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
std::string input = "";
int number(0);
while (true)
{
std::cout << "What is the transmition range?" ;
std::getline(std::cin, input);
std::stringstream ss(input);
if (ss >> number)
break;
std::cout << "Invalid number, please try again" << std::endl;
}
std::cout << "You entered: " << number << std::endl;
|
thanks that worked perfectly
Topic archived. No new replies allowed.