zoukankan      html  css  js  c++  java
  • c++学习笔记(cin输入类型不匹配的问题)

    题目:读取数字的循环。
    内容:使用cin输入数字,当发生类型不匹配的情况如何处理。
    要求:如果用户输入非数字输入,程序将拒绝,并要求用户继续输入数字。
    步骤:程序发现用户输入了错误内容时,应采取3个步骤:
                   1.重置cin以接受新的输入(cin.clear();)。
                   2.删除错误输入。
                   3.提示用户再输入。

     1 #include <iostream>
     2 const int Max=5;
     3 int main()
     4 {
     5     using namespace std;
     6     int golf[Max];
     7     cout<<"Please enter your golf scores.
    ";
     8     cout<<"You must enter "<<Max<<" rounds.
    ";
     9     int i;
    10     for(i=0;i<Max;i++)
    11     {
    12         cout<<"round #"<<i+1<<":";
    13         while(!(cin>>golf[i])){
    14             cin.clear();         //重置cin
    15             while(cin.get()!='
    ')         //删除错误输入
    16                 continue;
    17             cout<<"Please enter a number: ";  //提示用户再输入
    18         }
    19     }
    20     double total=0.0;
    21     for(i=0;i<Max;i++)
    22         total+=golf[i];
    23     cout<<total/Max<<"=average score "<< Max <<" rounds
    ";
    24     return 0;
    25 }
  • 相关阅读:
    hive 调优
    nohup
    安装ElasticSearch 6.1.1 head插件
    101. Symmetric Tree
    67. Add Binary
    70. Climbing Stairs
    896. Monotonic Array
    66. Plus One
    27. Remove Element
    Apache Tomcat文件包含漏洞风险大,威胁全球约8万台服务器
  • 原文地址:https://www.cnblogs.com/zhi321/p/11581808.html
Copyright © 2011-2022 走看看