Is there a way to specify the type of input a user can enter...
for example, I would like to have the user only allowed to enter a number from 0-9, and it wouldn't allow him to enter letters or symbols.
Printable View
Is there a way to specify the type of input a user can enter...
for example, I would like to have the user only allowed to enter a number from 0-9, and it wouldn't allow him to enter letters or symbols.
Not really, apart from inputing into a string, comparing each element to the values 0-9 then if they're all numbers, atoi() the string. It's quite a process though.
Ok, well now each time a user enters a character other than what is accepted by int, it terminates the program. So how would I go about fixing this one? I can't change the variable type because it is part of a switch function.
You could simulate it. Do what SlyMaelstrom said, compare the char to a number, if its not 0-9 then clear the screen, replace what was there and make them enter again.
I just told you what you can do, there is no point in rephrasing the question.
When you do something like
The user input HAS to be an integer, otherwise the stream will fail and all cin statements will be ignored for the rest of the program. There is no way around this. You can do what I said, it's just about the only way.Code:cin >> i1; //Where i1 is an int
k actually there are two ways i can think of that you can do that with.
first one is simple
and the other one is more comlicatedCode:...
int number;
do //do loop
{
cout<<"Please enter a number between 0-9\n";//asks for input
cin>>number; //puts it into that int
}while ( number < 0 && number > 9 ); //repeats it untill 0 to 9 is / //pressed
...
look up www.asciitable.comCode:include <conio>
..
int menu
menu=getch()
switch (menu)
{
case(27):stands for esc
{
cout<<"hello";
}
}
then have case for each one of the buttons
This does nothing. You've already input something into an int variable. If it was a character, the stream has already failed. ...and now that the stream is failed, you'd be stuck in an infinite loop...Quote:
Originally Posted by kennny2004
I don't see how this fixes his problem at all. All this does is say hello if the character pressed is the ascii value for escape. If you're insisting using this to check what character it is, you run into the problem of converting to an integer.Quote:
Originally Posted by kennny2004
I'm sorry that there is no simple way to do it, but there isn't. Trust me, every budding programmer has run into this issue and they all have to come to terms that there is no easy solution for it.
That is where ignore comes in.Quote:
Originally Posted by SlyMaelstrom
What are you gonna do? Ignore the input before you know what it is?
Ehh, using it along with clear, will get rid of the bad input, here is an example
Now, I'm both checking for invalid input, in the case that its out of range, and it will get rid of any bad data in the process. Now, if the bad data exceeds 256 characters, well the loop will keep going until it gets rid of all of it, but it will eventually.Code:#include <iostream>
using namespace std;
int main()
{
int input = -1;// initialize to invalid input
cout << "Input a number between 0 and 9 inclusive" << endl;
do
{
cin >> input;
if (input < 0 || input > 9)
{
cin.clear();
cin.ignore(256,'\n');
cout << "Please try again, the number must be between 0 and 9" << endl;
}
else
{
break; // if its valid break out
}
} while(true);
cout << "The value was " << input << endl;
}
BTW, google is helpfull, it gave me this upon searching
http://www.parashift.com/c++-faq-lit....html#faq-15.3
Thats were I found this information originally, and its a handy little faq. You might notice, they do it a little bit better then I do, with the while loop and all, but it works :)
Here is an example using what is called a "string stream":
Code:#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string input = "";
bool badInput = false;
do
{
cout<<"Please enter only digits: ";
cin>>input;
badInput = false;
for(int i = 0; i < input.length(); i++)
{
if(input[i] < '0' || input[i] > '9')
{
cout<<"Only digits 0-9!"<<endl;
badInput = true;
break;
}
}
}while(badInput);
istringstream str(input); //creates what's similar to an input file
//containing the string.
int num = 0;
str>>num; //read from the "input file" into the int variable
cout<<"You entered: " <<num<<endl;
return 0;
}
If the user types in something that does not fit the type your are expecting, the return value of >> will evaluate to false. This makes it easy to check for input that is invalid (like letters) as well as not what you want specifically (like greater than 9 or less than 0).Code:#include <iostream>
#include <limits>
int main()
{
using namespace std;
int input = -1;
cout << "Enter a number between 0 and 9: ";
while (!(cin >> input) || input < 0 || input > 9)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Try Again.\nEnter a number between 0 and 9: ";
}
cout << "Your number is: " << input << endl;
}