#include "stdafx.h"
#include <iostream>
const int max = 5;
int main()
{
using namespace std;
int golf[max];
cout << "Please enter your golf scores.\n";
cout << "You must 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 (i = 0; i < max; i++)
total += golf[i];
cout << total / max << " = average score" << max << " rounds \n";
return 0;
}
如果输入非数字输入,程序将拒绝,并要求用户继续输入数字。
当输入dafdafa45的时候没问题
可是输入了4dafda的时候就有问题了
发现4已经被存到了golf[1]中;
请问要怎么改才能改进这个程序。。。
#include <iostream>
const int max = 5;
int main()
{
using namespace std;
int golf[max];
cout << "Please enter your golf scores.\n";
cout << "You must 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 (i = 0; i < max; i++)
total += golf[i];
cout << total / max << " = average score" << max << " rounds \n";
return 0;
}
如果输入非数字输入,程序将拒绝,并要求用户继续输入数字。
当输入dafdafa45的时候没问题
可是输入了4dafda的时候就有问题了

发现4已经被存到了golf[1]中;
请问要怎么改才能改进这个程序。。。