所以这是我正在研究的问题
带输入验证(C ++)的成绩。使用数组和函数修改成绩程序。 (该程序应该最低限度地具有平均,最小,最大,#高于平均等的功能,总共4个功能)添加逻辑(到主),以便如果用户输入无效等级(低于零,高于100) ,将向用户发出错误消息,然后允许重新输入成绩。只要用户输入无效成绩,这将重复。你需要一个While循环。
当我输入一个小于0的数字时,由于某种原因它没有在while循环中执行代码,而且我的最小函数也不起作用
这是我对主要功能和最小功能的代码
#include <iostream>
using namespace std;
double average(double x[], int n);
double maximum(double x[], int n);
double minimum(double x[], int n);
int nAboveAvg(double x[], int n);
int main()
{
double grades[50];
int ngrades;
cout<<"How many grades? (max = 50) ";
cin>>ngrades;
//create for loop to get grades from user
for(int i = 0; i<ngrades; i++)
{
cout<<"Enter grade ";
cin>> grades[i];
while(grades[i]< 0 || grades[i] > 100)
{
cout<<"Invalid grade- please enter again"<<endl;
cin>>grades[i];
}
}
//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
double min = minimum(grades, ngrades);
int nAbove = nAboveAvg(grades, ngrades);
//display results
cout << "Average = " << avg << endl;
cout << "# above average = " << nAbove << endl;
cout<<"Max value is = "<<max<<endl;
cout<<"Min value is = "<<min<<endl;
}
//Minimum function
double minimum(double x[], int npts)
{
double min = x[0];
for(int i = 0; i < npts; i++)
{
if(x[i] < min)
{
min = x[i];
}
}
return min;
}