| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"TechNovice" |
| Date: |
15 Oct 2003 08:16:16 PM |
| Object: |
testing input values |
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.
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: testing input values |
15 Oct 2003 08:43:51 PM |
|
|
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:yjmjb.91087$k74.57013@lakeread05...
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 <ios>
******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
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: (corr) testing input values |
15 Oct 2003 08:47:06 PM |
|
|
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:rJmjb.2562$s93.2279@newsread3.news.pas.earthlink.net...
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:yjmjb.91087$k74.57013@lakeread05...
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.
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
.
|
|
|
|
| User: "TechNovice" |
|
| Title: Re: testing input values |
15 Oct 2003 08:48:20 PM |
|
|
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****
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:rJmjb.2562$s93.2279@newsread3.news.pas.earthlink.net...
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:yjmjb.91087$k74.57013@lakeread05...
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 <ios>
******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
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: testing input values |
16 Oct 2003 12:33:49 AM |
|
|
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:CNmjb.91099$k74.71902@lakeread05...
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
.
|
|
|
| User: "TechNovice" |
|
| Title: Re: testing input values |
16 Oct 2003 03:47:02 PM |
|
|
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;
}
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:15qjb.2612$7a4.1310@newsread4.news.pas.earthlink.net...
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:CNmjb.91099$k74.71902@lakeread05...
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
.
|
|
|
| User: "=?iso-8859-1?Q?Juli=E1n?= Albo" |
|
| Title: Re: testing input values |
16 Oct 2003 03:59:44 PM |
|
|
TechNovice escribi=F3:
bool valid(string input_number)
{
for(int i=3D0; i<x.length(); i++)
{
if(isdigit(x.at(i)) =3D=3D 0) //checks everything in the stri=
ng
return false;
}
return true;
}
You can do also:
if (x.find_first_not_of ("0123456789") !=3D string::npos)
return false;
But I suspect input_number will be used instead of x ;)
Regards.
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: testing input values |
16 Oct 2003 05:01:06 PM |
|
|
"Julián Albo" <JULIANALBO@terra.es> wrote in message
news:3F8F06C0.FF014949@terra.es...
You can do also:
if (x.find_first_not_of ("0123456789") != string::npos)
which is exactly the inverse of what I showed him for
prohibiting digits in input text. :-)
-Mike
.
|
|
|
| User: "=?iso-8859-1?Q?Juli=E1n?= Albo" |
|
| Title: Re: testing input values |
16 Oct 2003 05:08:10 PM |
|
|
Mike Wahler escribi=F3:
You can do also:
if (x.find_first_not_of ("0123456789") !=3D string::npos)
=
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.
.
|
|
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: testing input values |
16 Oct 2003 05:01:05 PM |
|
|
"Julián Albo" <JULIANALBO@terra.es> wrote in message
news:3F8F06C0.FF014949@terra.es...
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.
.
|
|
|
|
|
|
|
| User: "TechNovice" |
|
| Title: Re: testing input values |
15 Oct 2003 08:57:37 PM |
|
|
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.
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:CNmjb.91099$k74.71902@lakeread05...
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****
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:rJmjb.2562$s93.2279@newsread3.news.pas.earthlink.net...
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:yjmjb.91087$k74.57013@lakeread05...
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 <ios>
******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
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: testing input values |
16 Oct 2003 03:11:12 AM |
|
|
"TechNovice" <aparicion25@hotmail.com> wrote in message
news:jWmjb.91103$k74.27929@lakeread05...
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
.
|
|
|
|
|
|
|

|
Related Articles |
|
|