知识库

C++语言开发示例:04. 数据输入2

2025-02-01 20:29:54 李腾 1 次阅读

cingolf.cpp

#include <iostream>

const int Max = 5;
int main()
{
    using namespace std;
    int golf[Max];
    cout << "Please enter your golf scores.\n";
    cout << "You muse enter " << Max << " rounds.\n";
    int i;
    for (i = 0; i < Max; i++)
    {
        cout << "round #" << i + 1 << ": ";
        while (!(cin >> golf[i]))
        {
            cin.clear();
            while (cin.get() != '\n')
            {
                continue;
            }
            cout << "Please enter a number: ";
        }
    }
    double total = 0.0;
    for (int i = 0; i < Max; i++)
    {
        total += golf[i];
    }
    cout << total / Max << " = average score "
         << Max << " rounds\n";
}
转载请注明转自www.hylab.cn,原文地址:C++语言开发示例:04. 数据输入2

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!