testing input values

T

TechNovice

Hi:

I'm trying to find a way to test input values.
To test an integer I tried this code:
******Code******
int input_number;
cin>>input_number;
while(!input_number)
cout<<"invalid input"<<endl;
******End Code******
when I enter a character or a string I don't get the error message. I would
appreciate if someone could help me solve this problem.

Thanks.
 
M

Mike Wahler

TechNovice said:
Hi:

I'm trying to find a way to test input values.
To test an integer I tried this code:
******Code******
int input_number;
cin>>input_number;
while(!input_number)

Test the stream, not the int object. If the read
failed, the int object was not affected.

while(!cin)
{
cout<<"invalid input"<<endl;

// then before we can read more data we must
// reset the stream state:

cin.clear();

// and extract and discard the offending characters:
cin.ignore(numeric_limits<streamsize>::max(), '\n');

'numeric_limits' is declared by <limits>
'streamsize' is declared by said:
******End Code******
when I enter a character or a string I don't get the error message. I would
appreciate if someone could help me solve this problem.

HTH,
-Mike
 
M

Mike Wahler

Mike Wahler said:
Test the stream, not the int object. If the read
failed, the int object was not affected.

Not always true. e.g.

Enter an int: 123X

123 is assigned, and the stream goes into 'fail' state
(!cin returns true) when it encounters the 'X' . So you
would get the int changed but not necessarily to what you
expected. Perhaps you meant to type 1234.

-Mike
 
T

TechNovice

Thanks for your response, but I found that this works very well
****code****
int input_number;
if(cin>>input_number)
cout<<"valid number"<<endl;
else
cout<<"invalid number"<<endl;
****end of code****
 
T

TechNovice

that is true.
Is there a function to test for int, or char and fail when there is a
mixture of both types?
I need a function to check for numeric values anf fails when I enter a char
value or accepts only char values and fail when I enter int values.
 
M

Mike Wahler

TechNovice said:
Thanks for your response, but I found that this works very well
****code****
int input_number;
if(cin>>input_number)
cout<<"valid number"<<endl;
else
cout<<"invalid number"<<endl;

Yes, it does. This is because the expression 'cin>> input_number'
returns a reference to 'cin', so has the same testing effect
as 'if(cin)'. Indeed what you just showed me is the 'idomatic'
way to use the >> operator.

But now try it in a loop as in your original post. :)

(or simply try a subsequent 'cin' operation without
first doing the 'clear()' and 'ignore()')

-Mike
 
M

Mike Wahler

TechNovice said:
that is true.
Is there a function to test for int, or char and fail when there is a
mixture of both types?

No, but one can be written.
I need a function to check for numeric values anf fails when I enter a char
value

OK we've done this part already.
or accepts only char values and fail when I enter int values.

Think about this for a moment. The digits '1', '2', '3'
you type on your keyboard *are* characters. So of course
a character input routine will not reject them. But if
you do want do specify certain restrictions on a character
input, you can write the code to do so.

Also, you said "a function". Which type of data is being
input? It seems you're wanting to input two different
types at once. This doesn't make sense to me.

I think you need a function for numeric input
(which you have), and another for restrictive
character input:

#include <iostream>
#include <string>

void get_text(std::string& input, bool allow_digits = false)
{
do
{
std::cout << "Enter text "
<< (allow_digits
? ""
: "(no digits allowed)") << ": ";

std::getline(std::cin, input);
} while(!allow_digits &&
input.find_first_of("01234567890") != std::string::npos);
}

int main()
{
std::string s;

get_text(s, false);
std::cout << "You entered: " << s << '\n';

get_text(s, true);
std::cout << "You entered: " << s << '\n';

return 0;
}


BTW please don't top-post. Thanks.

-Mike
 
T

TechNovice

Thank you.
I also came up with another solution if anyone cares :)
#include <cstring>
#include <cctype>

string input_number; //numeric data
cin>>input_number;
while(!valid(input_number)) //validate input
cin>>input_number;

//valid function
bool valid(string input_number)
{
for(int i=0; i<x.length(); i++)
{
if(isdigit(x.at(i)) == 0) //checks everything in the string
return false;
}
return true;
}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

TechNovice escribió:
bool valid(string input_number)
{
for(int i=0; i<x.length(); i++)
{
if(isdigit(x.at(i)) == 0) //checks everything in the string
return false;
}
return true;
}

You can do also:

if (x.find_first_not_of ("0123456789") != string::npos)
return false;

But I suspect input_number will be used instead of x ;)

Regards.
 
M

Mike Wahler

TechNovice escribió:
bool valid(string input_number)
{
for(int i=0; i<x.length(); i++)
{
if(isdigit(x.at(i)) == 0) //checks everything in the string
return false;
}
return true;
}

You can do also:

if (x.find_first_not_of ("0123456789") != string::npos)
return false;

But I suspect input_number will be used instead of x ;)

Regards.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Mike Wahler escribió:
which is exactly the inverse of what I showed him for
prohibiting digits in input text. :)

Mmm... seems I don't pay attention to the complete thread.

By the way, the method the OP proposes does not mark as invalid an empty
string.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top