麻烦会的人帮我一下
如果往score[max_length]中输入不是数字的时候,让用户重新输入直到数据合法,这个功能该怎样做啊?希望会的帮下忙代码:
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){
const int max_length=100;
string name[max_length];
int score[max_length]={0};
double average=0.0;
char sign=0;
int count=0;
do{
cout<<"Please enter the name: ";
cin>>name[count];
cout<<"Please enter the score(0-100): ";
cin>>score[count];
do{
if(score[count]>=0&&score[count]<=100) break;
else if(score[count]<0||score[count]>100){
cout<<endl
<<"The score you enter is "
<<"more than 100 or less than 0!\nTry again!\n";
cout<<"Please enter the score(0-100): ";
cin>>score[count];
continue;
}
}while(true);
average+=score[count];
count++;
cout<<"Do you want to enter another(y or n)? ";
cin>>sign;
cout<<endl;
}while(count<100&&std::tolower(sign)=='y');
average/=count;
for(int i=0;i<count;i++){
cout<<"name: "<<name[i];
cout<<"\tscore: "<<score[i]<<endl;
}
cout<<endl<<"Average is: "<<std::fixed<<average<<endl;
system("pause");
return 0;
}