//Devin Scheu CS1A Chapter 6, P. 370, #4
//
/**************************************************************
*
* DETERMINE SAFEST DRIVING AREA BY ACCIDENTS
* ____________________________________________________________
* This program identifies the geographic region with the
* fewest reported automobile accidents last year.
* ____________________________________________________________
* INPUT
* regionAccidents : The number of accidents for each region
*
* OUTPUT
* safestRegion : The name and accident figure of the region with the fewest accidents
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function to get number of accidents for a region
int getNumAccidents(string regionName) {
int accidents;
cout << "Enter the number of accidents for " << regionName << ": ";
cin >> accidents;
while (accidents < 0) {
cout << "\nError: Please enter a non-negative number: ";
cin >> accidents;
}
cout << accidents << endl;
return accidents;
}
// Function to find the region with the lowest accidents
void findLowest(int north, int south, int east, int west, int central) {
int lowestAccidents = north;
string safestRegion = "North";
if (south < lowestAccidents) {
lowestAccidents = south;
safestRegion = "South";
}
if (east < lowestAccidents) {
lowestAccidents = east;
safestRegion = "East";
}
if (west < lowestAccidents) {
lowestAccidents = west;
safestRegion = "West";
}
if (central < lowestAccidents) {
lowestAccidents = central;
safestRegion = "Central";
}
cout << left << setw(25) << "Safest Region:" << right << setw(15) << safestRegion << endl;
cout << left << setw(25) << "Number of Accidents:" << right << setw(15) << lowestAccidents << endl;
}
int main () {
//Variable Declarations
int northAccidents; //INPUT - The number of accidents for North region
int southAccidents; //INPUT - The number of accidents for South region
int eastAccidents; //INPUT - The number of accidents for East region
int westAccidents; //INPUT - The number of accidents for West region
int centralAccidents; //INPUT - The number of accidents for Central region
string safestRegion; //OUTPUT - The name and accident figure of the region with the fewest accidents
//Get Accidents for Each Region
northAccidents = getNumAccidents("North");
southAccidents = getNumAccidents("South");
eastAccidents = getNumAccidents("East");
westAccidents = getNumAccidents("West");
centralAccidents = getNumAccidents("Central");
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Find and Display Safest Region
findLowest(northAccidents, southAccidents, eastAccidents, westAccidents, centralAccidents);
} //end of main()