Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > testing input values

Reply
Thread Tools

testing input values

 
 
TechNovice
Guest
Posts: n/a
 
      10-16-2003
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.



 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-16-2003

"TechNovice" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-16-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:rJmjb.2562$(E-Mail Removed) ink.net...
>
> "TechNovice" <(E-Mail Removed)> 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


 
Reply With Quote
 
TechNovice
Guest
Posts: n/a
 
      10-16-2003
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" <(E-Mail Removed)> wrote in message
news:rJmjb.2562$(E-Mail Removed) ink.net...
>
> "TechNovice" <(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
TechNovice
Guest
Posts: n/a
 
      10-16-2003
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" <(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in message
> news:rJmjb.2562$(E-Mail Removed) ink.net...
> >
> > "TechNovice" <(E-Mail Removed)> 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
> >
> >

>
>



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-16-2003

"TechNovice" <(E-Mail Removed)> 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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-16-2003

"TechNovice" <(E-Mail Removed)> 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


 
Reply With Quote
 
TechNovice
Guest
Posts: n/a
 
      10-16-2003
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" <(E-Mail Removed)> wrote in message
news:15qjb.2612$(E-Mail Removed) ink.net...
>
> "TechNovice" <(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
=?iso-8859-1?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2003
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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-16-2003

"Julián Albo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing testing 123 neville Computer Support 7 06-28-2005 05:06 PM
testing testing neville Computer Support 16 06-05-2005 09:02 PM
testing testing neville Computer Support 2 05-27-2005 09:57 AM
testing testing 123 daniel edwards Computer Support 4 05-20-2004 10:36 PM
testing--news2004--testing Boomer Computer Support 3 09-24-2003 06:54 PM



Advertisments