fork download
  1. //Vanessa Ramirez CSC5 Chapter 6, P. 370, #4
  2. //
  3. /*******************************************************************************
  4.  * FEWEST AUTOMOBILE ACCIDENTS
  5.  * _____________________________________________________________________________
  6.  * This program which five geographic regions had the fewest reported automobile
  7.  * accidents. This program asks the user for the accident total in each region,
  8.  * valdiates teh input, and displays the safest region.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * north - Number of accidents in the north region
  12.  * south - Number of accidents in the south region
  13.  * east - Number of accidents in the east region
  14.  * west - Number of accidents in the west region
  15.  * central - Number of accidents in the central region
  16.  *
  17.  * OUTPUT
  18.  * safestRegion - Region with the fewest accidents
  19.  * lowest - Lowest number of accidents
  20.  ******************************************************************************/
  21. #include <iostream>
  22. #include <string>
  23. using namespace std;
  24.  
  25. //Function prototype
  26. int getNumAccidents (string region);
  27. void findLowest (int north, int south, int east, int west, int central);
  28.  
  29. int main()
  30. {
  31. // Declare variables
  32. int north;
  33. int south;
  34. int east;
  35. int west;
  36. int central;
  37.  
  38. // Get accident totals for each region
  39. north = getNumAccidents("North");
  40. south = getNumAccidents("South");
  41. east = getNumAccidents("East");
  42. west = getNumAccidents("West");
  43. central = getNumAccidents("Central");
  44.  
  45. // Find and display the region with fewest accidents
  46. findLowest(north, south, east, west, central);
  47.  
  48. return 0;
  49. }
  50. // getNumAccidents
  51. // Ask for region's accident total., validate the input, and returns the number
  52. // of accidents.
  53. //
  54. //******************************************************************************
  55. int getNumAccidents(string region)
  56. {
  57. int accidents;
  58. cout << "Enter the number of accidents for the "
  59. << region << " region: ";
  60. cin >> accidents;
  61.  
  62. while (accidents < 0)
  63. {
  64. cout << "Accidents cannot be less than 0." << endl;
  65. cout << "Enter the number of accidents for the "
  66. << region << " region: ";
  67. cin >> accidents;
  68. }
  69. return accidents;
  70. }
  71.  
  72. void findLowest(int north, int south, int east, int west, int central)
  73. {
  74. int lowest;
  75. string safestRegion;
  76.  
  77. //Assume the north region has the fewest accidents
  78. lowest = north;
  79. safestRegion = "North";
  80.  
  81. if (south < lowest)
  82. {
  83. lowest = south;
  84. safestRegion = "South";
  85. }
  86. if (east < lowest)
  87. {
  88. lowest = east;
  89. safestRegion = "East";
  90. }
  91. if (west < lowest)
  92. {
  93. lowest = west;
  94. safestRegion = "West";
  95. }
  96. if (central < lowest)
  97. {
  98. lowest = central;
  99. safestRegion = "Central";
  100. }
  101.  
  102. // Display the results
  103. cout << endl << "Safest Driving Area: " << safestRegion << endl;
  104. cout << "Number of accidents: " << lowest << endl;
  105. }
Success #stdin #stdout 0s 5308KB
stdin
25
18
32
12
20
stdout
Enter the number of accidents for the North region: Enter the number of accidents for the South region: Enter the number of accidents for the East region: Enter the number of accidents for the West region: Enter the number of accidents for the Central region: 
Safest Driving Area: West
Number of accidents: 12