房天下 > 房天下问答 > 业主生活 > 其他
  • c++ 求一段代码

    要求输入一个数字,为了避免输入其它字符造成死循环,要做检验~代码怎样写啊?不用string~

    提问者:真彩名片

    发布于2011-01-25

共1个回答
  • 快乐男主妇 丨Lv 1
    #include<iostream>using namespace std;void main(){ int x; cout<<"Made by 小恩"<<endl<<"Enter a munber: "<<endl; while(1) { cin>>x; if(cin.fail()) { cin.clear(); while('\n'!=getchar())//忽略错误输入 ; cout<<"Error input,Try again!"<<endl; } else { while('\n'!=getchar())//忽略正确输入后的流结束字符和无用字符 ; break; } } cout<<"The number is "<<x<<endl<<"Thank you for your use!"<<endl;}/*result:Made by 小恩Enter a munber:dfgdfgsError input,Try again!4565456345634563456456Error input,Try again!t556rertError input,Try again!45The number is 45Thank you for your use!请按任意键继续. . .*/
    +1 2011-01-25 举报
热门人气推荐
免责声明:问答内容均来源于互联网用户,房天下对其内容不负责任,如有版权或其他问题可以联系房天下进行删除。