Advertisement
hfxdesign

Horse Stable

Oct 12th, 2014
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. /*
  2. Name: Nickelis Jarvis
  3. Date: 10.10.14
  4. Program Name: Horse Stable
  5. Description: Check if a horse is over / under weight and calcualte feed accordingly.
  6. */
  7.  
  8. #include <iostream>
  9. #include <iomanip>
  10. #include <string>
  11.  
  12. //Default console width in windows is 80 characters, define a global constant for that.
  13. #define CONSOLE_MAXWIDTH 80
  14.  
  15. //Macro for getting the median element number of an array.
  16. #define ARRAY_MEDIAN(array_name) ((sizeof(array_name) / sizeof(array_name[0]) + (sizeof(array_name) % sizeof(array_name[0])) ? 1: 0))
  17.  
  18. int main() {
  19.  
  20.     //Create string variable for horse type.
  21.     std::string horseType;
  22.  
  23.     //Create integer variable for horse weight.
  24.     int horseWeight;
  25.  
  26.  
  27.     //Create a structure array for the 3 types of horse { Light, Large, and Draft }.
  28.     const struct {
  29.         const char* breed;
  30.         const int weight_min;
  31.         const int weight_max;
  32.     } horse[3] = {
  33.         "Light",    //Breed
  34.         840,        //Minimum Weight
  35.         1200,       //Maximum Weight.
  36.  
  37.         "Large",
  38.         1100,
  39.         1300,
  40.  
  41.         "Draft",
  42.         1500,
  43.         2200
  44.     }, *p_horse = &horse[0]; //Declare a pointer to the first element of the array, good practice.
  45.  
  46.     //Create a struct for the weight categories and feed amounts.
  47.     const struct {
  48.         const char* weightCat;
  49.         const double feedAmt;
  50.     }feedGuide[3] = {
  51.         "Underweight"//Weight
  52.         3.3,            //Feed Amount
  53.  
  54.         "Optimal",
  55.         3.0,
  56.  
  57.         "Overweight",
  58.         2.5
  59.     }, *p_feedGuide = &feedGuide[ARRAY_MEDIAN(feedGuide)]; //Create pointer and set to median element (in this case, [1]).
  60.  
  61.     //Create a variable to set column width based on number of columns we will be using. In this case, 3.
  62.     //(Note that int does truncate, this was intentional).
  63.     const unsigned int colWidth = (CONSOLE_MAXWIDTH / 3);
  64.    
  65.     //Output title and program header.
  66.     std::cout
  67.         << "Nick's Horse Stables \n\n"
  68.         << std::setw(80) << std::setfill('=') << '\n'
  69.         << std::setfill(' ') << std::left << std::setw(colWidth) << "Horse Type" << std::setw(colWidth) << "Minimum optimum weight" << std::setw(colWidth) << "Maximum optimum weight" << std::endl
  70.         << std::setw(colWidth) << "" << std::setw(colWidth) << "(range in pounds)" << std::setw(colWidth) << "(range in pounds)\n" << std::endl;
  71.  
  72.     //Use a for loop to output info on horse types pulled from the horse struct.
  73.     //Use and increment the pointer we created to scan through the array.
  74.     for (int i = 0; i <= 2; i++, p_horse++) {
  75.         std::cout
  76.             << std::setw(colWidth) << (*p_horse).breed
  77.             << std::setw(colWidth) << (*p_horse).weight_min
  78.             << std::setw(colWidth) << (*p_horse).weight_max << '\n';
  79.     }
  80.     //Reset the pointer to member [0] of our array.
  81.     p_horse = &horse[0];
  82.     std::cout << std::setw(80) << std::setfill('=') << '\n' << std::endl;
  83.  
  84.     //Ask for the type of horse.
  85.     std::cout << "\nEnter horse type: ";
  86.  
  87.     //Get horse type, then use a loop with the pointer to horse[] to scan and find the type of horse we're working with.
  88.     std::getline(std::cin, horseType);
  89.  
  90.     for (int i = 0; i <= 2; i++, p_horse++) {
  91.         if ((horseType.compare((*p_horse).breed) == 0)) {
  92.             //Get weight of horse and compare, check if it is out of optimal range, if so, move the pointer to appropriate weight category and feed amount.
  93.             //This creates a default setting of 'Optimal' in the feed range, so nothing need be done if it's already there.
  94.             std::cout << "Enter horse weight: ";
  95.             std::cin >> horseWeight;
  96.             if (horseWeight < (*p_horse).weight_min || horseWeight >(*p_horse).weight_max) {
  97.                 if (horseWeight < (*p_horse).weight_min)
  98.                     p_feedGuide--;
  99.                 else if (horseWeight > (*p_horse).weight_max)
  100.                     p_feedGuide++;
  101.             }
  102.             std::cout
  103.                 << "Horse type: " << (*p_horse).breed << '\n'
  104.                 << "Horse weight: " << horseWeight << '\n'
  105.                 << "Weight category: " << (*p_feedGuide).weightCat << '\n'
  106.                 << std::fixed << std::setprecision(1) << "Feed amount: " << (*p_feedGuide).feedAmt << '\n' << std::endl;
  107.             break; //Everything done, break the loop so we can exit!
  108.         }
  109.         else if (i >= 2) {
  110.             std::cout << "Invalid horse type." << std::endl;
  111.             break; //Break loop to exit.
  112.         }
  113.     }
  114.     system("pause"); //Pause before exiting.
  115.     return(0);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement