473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Jul 19 '05 #1
11 7224

"TechNovice" <ap*********@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
Jul 19 '05 #2

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:rJ*****************@newsread3.news.pas.earthl ink.net...

"TechNovice" <ap*********@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
Jul 19 '05 #3
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" <mk******@mkwahler.net> wrote in message
news:rJ*****************@newsread3.news.pas.earthl ink.net...

"TechNovice" <ap*********@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

Jul 19 '05 #4
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" <ap*********@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" <mk******@mkwahler.net> wrote in message
news:rJ*****************@newsread3.news.pas.earthl ink.net...

"TechNovice" <ap*********@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


Jul 19 '05 #5

"TechNovice" <ap*********@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
Jul 19 '05 #6

"TechNovice" <ap*********@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
Jul 19 '05 #7
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" <mk******@mkwahler.net> wrote in message
news:15*****************@newsread4.news.pas.earthl ink.net...

"TechNovice" <ap*********@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

Jul 19 '05 #8
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.
Jul 19 '05 #9

"Julián Albo" <JU********@terra.es> wrote in message
news:3F***************@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.
Jul 19 '05 #10
"Julián Albo" <JU********@terra.es> wrote in message
news:3F***************@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
Jul 19 '05 #11
Mike Wahler escribió:
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. :-)


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.
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Allen | last post by:
We have found that our method of testing does not match traditional text-book methodologies. I decided to write a short white paper on it so that I could get your opinions. Does anyone else use...
0
by: ImraneA | last post by:
Hi there I had pleasure of upsizing Access v97 db to Access v2K/SQL 2K. Wish to provide some knowledge gained back to community - hopefully help others. 1.Question how do you test stored...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
2
by: spgmbl | last post by:
I have set up the local environment to use sqlserver mode testing. The article i followed to install was here: http://support.microsoft.com/default.aspx?scid=kb;en-us;317604 I also changed the...
3
by: Water Cooler v2 | last post by:
Here's my understanding as of now. If I were writing a function bool IsValidContact(Offerer objOfferer, Accepter objAccepter, TermsAndConditions objTermsAndConditions); Before writing the...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
4
by: fish | last post by:
Consider this page: http://college.hmco.com/esl/vestri_solomon/top20/1e/students/exercises/unit16/exe03.html A user is asked to select a relative pronoun from a drop down list, and, if all the...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have setup UnitTests that go thru a SQL Server Table Sequentially using the UntTest Property for DataSource. What if I need to use 2 tables - I have a Parent child table that I need to...
0
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.