//Andres Guzman CSC5 Chapter 6, P. 370, #4
//
/*******************************************************************************
*
* DETERMINE SAFEST REGION
* ______________________________________________________________________________
* This program will determine the safest of 5 regions among car crashes
*
* Computation:
*
* region < lowest
* ______________________________________________________________________________
* INPUT
*
* north = saves north region function
* south = saves south region function
* east = saves east region function
* west = saves west region function
* central = saves central region function
* OUTPUT
* lowest = lowest amount of crashes
* lowestregion = region with lowest crashes reported
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
int getNumAccidents(string region); //Function Prototype
void findLowest(int n, int s, int e, int w, int c); //Function Prototype
int main()
{
//
//Displaying division and calling function for north
cout << "\tNorth Region";
int north = getNumAccidents("North"); //adding value of the function to
//int.
//
//Displaying division and calling function for south
cout << "\tSouth Region";
int south = getNumAccidents("South"); //adding value of the function to
//int.
//
//Displaying division and calling function for east
cout << "\tEast Region";
int east = getNumAccidents("East"); //adding value of the function to
//int.
//
//Displaying division and calling function for west
cout << "\tWest Region";
int west = getNumAccidents("West"); //adding value of the function to
//int.
//
//Displaying division and calling function for central
cout << "\tCentral Region";
int central = getNumAccidents("Central"); //adding value of the function to
//int.
//
//Calling function to determine lowest reported value
findLowest(north, south, east, west, central); //function call
return 0;
}
/*******************************************************************************
* Adding definition to function getNumAccidents. parameter is a string to pass
* the name of divisions. Will prompt an output to ask user for reported crashes
* and holds a while loop for invalid validation.
******************************************************************************/
int getNumAccidents(string region)
{
int accidents; //Input for reported accidents
cout << "\nReported Accidents Within Last Year: ";
cin >> accidents;
cout << endl;
//While loop for input validation of accidents
while (accidents < 0) //accidents cannot be less than 0
{
cout << "\nInvalid Input, please enter a number greater than 0.";
cout << "\nReported Accidents Within Last Year: ";
cin >> accidents;
cout << endl;
}
return accidents; //Returns value of accidents
}
/*******************************************************************************
* Add definition to function findLowest. Will determine which region was
* reported with the lowest value and then display both the region and their
* value.
******************************************************************************/
void findLowest(int n, int s, int e, int w, int c)
{
int lowest = n; //Assigns lowest value to north
string lowestregion = "North"; //Assigns lowestregion to North
//
//If statement initialized for determining lowest region
if (s < lowest) //If division is lowest, continue with following statement
{
lowest = s; //Replaces lowest value
lowestregion = "South"; //Replaces lowest region
}
if (e < lowest)//If division is lowest, continue with following statement
{
lowest = e; //Replaces lowest value
lowestregion = "East"; //Replaces lowest region
}
if (w < lowest)//If division is lowest, continue with following statement
{
lowest = w; //Replaces lowest value
lowestregion = "West"; //Replaces lowest region
}
if (c < lowest)//If division is lowest, continue with following statement
{
lowest = c; //Replaces lowest value
lowestregion = "Central"; //Replaces lowest region
}
//
//Output results
cout << "Region with Lowest Reported Accidents: " << lowestregion;
cout << "\nAccident Figure: " << lowest;
}