1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/* Write a program that determines which of five geographic regions within a major city (north, south, east, west and central)
had the fewest reported automobile accidents last year. It should have the following two functions, which are called by main.
int getNumAccidents() is passed the name of a region. It asks the user for the number of automobile accidents reported in that
region during the last year, validates the input, then returns it. It should be called once for each city region. void findLowest()
is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its
accident figure. Input Validation: Do not accept a number that is negative.*/
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//function prototypes
int getNumAccidents(int NumAccidents);
void findLowest(int north,int south,int east,int west,int central); //global variables
int NumAccidents;
int main()
{ //defining variables
int getNumAccidents(string regionName);
string regName;
int north,
south,
east,
west,
central,
NumAccidents;
cout<<"There are 5 major regions within the city.\n";
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);
system("pause");
return 0;
} //End of main function
int getNumAccidents(string regionName)
{
int NumAccidents; //hold values
do
{
cout << "Enter the number of accidents for the " << regionName;
cin >> NumAccidents;
} while (NumAccidents < 0); //validate the input
return NumAccidents; //return number of accidents
} //End of function getNumAccidents
void findLowest(int north, int south, int east, int west, int central)
{
string regName;
int crash = north;
{
if (crash > south)
{
regName = south;
cout<<"South";
crash = south;
}
else if (crash > east)
{
regName = east;
cout<<"East";
crash = east;
}
else if (crash > west)
{
regName = west;
cout<<"West";
crash = west;
}
else if (crash > central)
{
regName = central;
cout<<"Central";
crash = central;
}
}
cout << " is the region with the least amount of accidents with " << crash << " acciddents.";
} //End of function findLowest
|