Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nickelis Jarvis
- Date: 10.10.14
- Program Name: Horse Stable
- Description: Check if a horse is over / under weight and calcualte feed accordingly.
- */
- #include <iostream>
- #include <iomanip>
- #include <string>
- //Default console width in windows is 80 characters, define a global constant for that.
- #define CONSOLE_MAXWIDTH 80
- //Macro for getting the median element number of an array.
- #define ARRAY_MEDIAN(array_name) ((sizeof(array_name) / sizeof(array_name[0]) + (sizeof(array_name) % sizeof(array_name[0])) ? 1: 0))
- int main() {
- //Create string variable for horse type.
- std::string horseType;
- //Create integer variable for horse weight.
- int horseWeight;
- //Create a structure array for the 3 types of horse { Light, Large, and Draft }.
- const struct {
- const char* breed;
- const int weight_min;
- const int weight_max;
- } horse[3] = {
- "Light", //Breed
- 840, //Minimum Weight
- 1200, //Maximum Weight.
- "Large",
- 1100,
- 1300,
- "Draft",
- 1500,
- 2200
- }, *p_horse = &horse[0]; //Declare a pointer to the first element of the array, good practice.
- //Create a struct for the weight categories and feed amounts.
- const struct {
- const char* weightCat;
- const double feedAmt;
- }feedGuide[3] = {
- "Underweight", //Weight
- 3.3, //Feed Amount
- "Optimal",
- 3.0,
- "Overweight",
- 2.5
- }, *p_feedGuide = &feedGuide[ARRAY_MEDIAN(feedGuide)]; //Create pointer and set to median element (in this case, [1]).
- //Create a variable to set column width based on number of columns we will be using. In this case, 3.
- //(Note that int does truncate, this was intentional).
- const unsigned int colWidth = (CONSOLE_MAXWIDTH / 3);
- //Output title and program header.
- std::cout
- << "Nick's Horse Stables \n\n"
- << std::setw(80) << std::setfill('=') << '\n'
- << std::setfill(' ') << std::left << std::setw(colWidth) << "Horse Type" << std::setw(colWidth) << "Minimum optimum weight" << std::setw(colWidth) << "Maximum optimum weight" << std::endl
- << std::setw(colWidth) << "" << std::setw(colWidth) << "(range in pounds)" << std::setw(colWidth) << "(range in pounds)\n" << std::endl;
- //Use a for loop to output info on horse types pulled from the horse struct.
- //Use and increment the pointer we created to scan through the array.
- for (int i = 0; i <= 2; i++, p_horse++) {
- std::cout
- << std::setw(colWidth) << (*p_horse).breed
- << std::setw(colWidth) << (*p_horse).weight_min
- << std::setw(colWidth) << (*p_horse).weight_max << '\n';
- }
- //Reset the pointer to member [0] of our array.
- p_horse = &horse[0];
- std::cout << std::setw(80) << std::setfill('=') << '\n' << std::endl;
- //Ask for the type of horse.
- std::cout << "\nEnter horse type: ";
- //Get horse type, then use a loop with the pointer to horse[] to scan and find the type of horse we're working with.
- std::getline(std::cin, horseType);
- for (int i = 0; i <= 2; i++, p_horse++) {
- if ((horseType.compare((*p_horse).breed) == 0)) {
- //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.
- //This creates a default setting of 'Optimal' in the feed range, so nothing need be done if it's already there.
- std::cout << "Enter horse weight: ";
- std::cin >> horseWeight;
- if (horseWeight < (*p_horse).weight_min || horseWeight >(*p_horse).weight_max) {
- if (horseWeight < (*p_horse).weight_min)
- p_feedGuide--;
- else if (horseWeight > (*p_horse).weight_max)
- p_feedGuide++;
- }
- std::cout
- << "Horse type: " << (*p_horse).breed << '\n'
- << "Horse weight: " << horseWeight << '\n'
- << "Weight category: " << (*p_feedGuide).weightCat << '\n'
- << std::fixed << std::setprecision(1) << "Feed amount: " << (*p_feedGuide).feedAmt << '\n' << std::endl;
- break; //Everything done, break the loop so we can exit!
- }
- else if (i >= 2) {
- std::cout << "Invalid horse type." << std::endl;
- break; //Break loop to exit.
- }
- }
- system("pause"); //Pause before exiting.
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement