C++中关于流的概念
2011-12-15 21:00
246 查看
怎么从键盘中读取一个整数并包括错误处理
int a;
while (cin.get(ch1)) {
if (ch1=='\n')
cout<<"input the right number:"; //第一次输入换行;
else if (cin.get(ch2),ch2=='\n') {
a=ch1-'0';
if (a>0 && a<10)
break;
else
cout<<"input the right number:"; //一位数字输入错误;
}
else if(cin.get(ch3),ch3=='\n'){
a=(ch1-'0')*10+ch2-'0';
if (a>0 && a<16)
break;
else
cout<<"input the right number:"; //两位数字输入错误;
}
else {
cout<<"input the right number:"; //多次输入错误,清除缓冲流;
while (cin.get(ch3),ch3!='\n')
;
}
}
int a;
while (cin.get(ch1)) {
if (ch1=='\n')
cout<<"input the right number:"; //第一次输入换行;
else if (cin.get(ch2),ch2=='\n') {
a=ch1-'0';
if (a>0 && a<10)
break;
else
cout<<"input the right number:"; //一位数字输入错误;
}
else if(cin.get(ch3),ch3=='\n'){
a=(ch1-'0')*10+ch2-'0';
if (a>0 && a<16)
break;
else
cout<<"input the right number:"; //两位数字输入错误;
}
else {
cout<<"input the right number:"; //多次输入错误,清除缓冲流;
while (cin.get(ch3),ch3!='\n')
;
}
}
相关文章推荐
- 关于C++中 is a概念的一点疑问,希望高手解答。
- 关于c++概念的一些理解
- 关于C++中指针的概念
- 【C++】:关于C++的这些概念你知道吗?
- 【c++程序】关于指针易混淆的几个概念
- c++关于虚表 图文并茂 不只是概念
- 12-4-19关于c++动态的概念 | 返回引用的情况
- C++中关于位域的概念
- 关于C++中的一些概念
- C++中关于类的概念的学习思路
- C++ 有关于谓词的概念 20180329day15
- C++中关于位域的概念
- C++基本概念——关于头文件的那些事
- C++中关于位域的概念
- C++中关于位域的概念
- 关于PADS的一些概念和实用技巧(二)
- 关于C++函数主题
- [原创] 一劳永逸:关于C/C++中指针、数组与函数复合定义形式的直观解释
- MSVC:关于编译、链接、装载、库相关的一些概念
- 关于C++函数返回数组