我有一个函数在输入验证后被调用。 它会检查分组大小的数字范围,它们不能小于1且不大于10,我也正在验证它用于无效字符,如字母和符号。while循环中的多个条件C++
int validateGroupSize(int groupsize[], int i)
{
while((groupsize[i] < 0 || groupsize[i] > 11) || (!(cin >> groupsize[i]))){
cout << string(60, '\n');
cout << "Please re-enter customer's group size. " << endl << "Input must be a number and must not be smaller than 1 and greater than 10: " << endl;
cin >> groupsize[i];
cin.clear();
cin.ignore(20, '\n');
}
return groupsize[i];
}
int main()
{
int groupsize[10]={0,0,0,0,0,0,0,0,0,0};
int i;
char newentry='n';
do{
cout << string(60, '\n');
cout << endl << endl << "Please enter customer's group size: " << endl;
groupsize[i]=validateGroupSize(groupsize, i);
i++;
cout << "Would you like to enter another questionare? Enter either 'y' or 'n': " << endl;
cin >> newentry;
}while((newentry =='y') || (newentry=='Y'));
system("pause");
return 0;
}
到目前为止程序工作的无效字符,因为当我输入一个字母,它会提示我的错误信息。不幸的是,当我输入一个大于10的数字时,它会简单地忽略条件并继续执行程序的其余部分。我做错了什么?
主要是什么'i'? – P0W
这应该从来没有编译过。 – Panzercrisis
对不起,我忘了在这里添加int i。我在int main的while循环中找到i ++来计算它循环了多少次。 – noobuser333