测试输入值 [英] testing input values
本文介绍了测试输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨:
我正试图找到一种测试输入值的方法。
为了测试一个整数,我尝试了这段代码:
******代码******
int input_number;
cin>> input_number;
while(!input_number)
cout<<<"""""<<<<< endl;
******结束代码**** **
当我输入字符或字符串时我没有收到错误消息。如果有人可以帮我解决这个问题,我会感激。$ />
谢谢。
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.
推荐答案
" TechNovice" < AP ********* @ hotmail.com>在消息中写道
news:yjmjb.91087
"TechNovice" <ap*********@hotmail.com> wrote in message
news:yjmjb.91087
k74.57013@lakeread05 ...
k74.57013@lakeread05...
嗨:
<我正在试图找到一种测试输入值的方法。
为了测试整数,我尝试了这段代码:
******代码******
int input_number;
cin>> input_number;
while(!input_number)
测试流,而不是int对象。如果读取
失败,则int对象不受影响。
while(!cin)
{
cout<<"无效输入"<< endl;
//然后在我们读取更多数据之前我们必须
//重置流状态:
cin.clear();
//并提取并丢弃有问题的字符:
cin.ignore(numeric_limits< streamsize> :: max(), ''\ n'');
''numeric_limits''由< limits>声明。
''streamsize''由<声明; ios>
******结束代码******
当我输入字符或字符串时,我不会收到错误消息。如果有人可以帮我解决这个问题,我会很感激。
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
HTH,
-Mike
" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道
news:rJ ***************** @ newsread3.news.pas.earthl ink.net ...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:rJ*****************@newsread3.news.pas.earthl ink.net...
TechNovice < AP ********* @ hotmail.com>在消息中写道
新闻:yjmjb.91087
"TechNovice" <ap*********@hotmail.com> wrote in message
news:yjmjb.91087
这篇关于测试输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文