dobest

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  1 Posts :: 2 Stories :: 0 Comments :: 0 Trackbacks

常用链接

留言簿(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.\n";
 8        cout << "You must enter " << Max << " rounds.\n";
 9        int i;
10        for ( i = 0; i < Max; i++) //允许输入5个数字
11        {
12            cout << "round #" << i + 1 << "";
13            while (!(cin >> golf[i])) { //检测输入是否返回整数数组,如果失败清除输入.
14                  cin.clear();
15                  while (cin.get()!= '\n') //如果没清除到行尾.继续执行cin.clear()
16                        continue;
17                  cout << "please enter  number: ";
18                  }

19            }

20            
21            double total = 0.0;
22            for ( i = 0; i < Max; i++)
23                total += golf[i];
24                
25                cout << total / Max << " = average score "
26                     << Max << " rounds\n";
27            system("pause");
28            return 0;
29    }

30            处理错误核心代码如下:
while (!(cin >> golf[i])) {
                  cin.clear();
                  
while (cin.get()!= '\n')
                        
continue;
                  cout 
<< "please enter  number: ";
                  }

posted on 2008-04-23 23:12 叶家明 阅读(156) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理