int getNumAccidents(string );
void findLowest(double , double , double , double , double);
int main()
{
string regName;
double north = 0;
double south = 0;
double east = 0;
double west = 0;
double central = 0;
double lowest;
regName = "North";
north = getNumAccidents(regName);
regName = "South";
south = getNumAccidents(regName);
regName = "East";
east = getNumAccidents(regName);
regName = "West";
west = getNumAccidents(regName);
regName = "Central";
central = getNumAccidents(regName);
findLowest(north, south, east, west, central);
return 0;
}
getNumAccidents 函数允许用户输入每个地区报告的事故数量。
int getNumAccidents(string regionName)
{
double number;
cout << "\nEnter number of accidents for " << regionName << " area: " << endl;
cin >> number;
while (number <= -1)
{
cout << "\nEnter a positive number: " << endl;
cin >> number;
}
//cout << "Region: " << regionName << " Number: " << number << endl;
return number;
}
findLowest 函数确定哪个地区的事故数量最少以及该地区的名称。但是如何让它显示事故数量最少的地区名称呢?
void findLowest(double n, double s, double e, double w, double c)
{
double lowest = n;
if (s < lowest)
{
lowest = s;
}
if (e < lowest)
{
lowest = e;
}
if (w < lowest)
{
lowest = w;
}
if (c < lowest)
{
lowest = c;
}
cout << "\nThe least number of accidents was " << lowest << "." << endl;
//cout << "The area was " << r << "." << endl;
}