Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//Example program #include <iostream> #include <string> #include <iomanip> using

ID: 3730361 • Letter: #

Question



//Example program #include <iostream> #include <string> #include <iomanip> using namespace std;
const float WOLF_PRICE = 55.00; const float RABBIT_PRICE = 15.00; const float STEAK_PRICE = 12.00; //all of the above are measured in $/yd^3

//--------------------------------------------------------------------------- // Name: GetYorN // Parameters: Questions, string, input, the YN question to ask // Returns: char; the character the user entered. Must be 'y' or 'Y' or 'n' or 'N' // Purpose: This function returns the user's response to a yes/no question //--------------------------------------------------------------------------- char GetYorN(const string Question) { char choice;
cout << Question << " Enter Y or N: "; cin >> choice; choice = toupper(choice);
while (choice != 'Y' && choice != 'N') { cout << "Invalid character. Please enter either (Y) for yes or (N) for no. "; cout << Question << " Enter Y or N: "; cin >> choice; choice = toupper(choice); }
return (choice); }
//--------------------------------------------------------------------------- // Name: GetLoadChoice // Parameters: reference char choice // Returns: none (passed in via reference), Must be 'R', 'W', or 'S' // Purpose: Determine which of the items the user would like to ship // //--------------------------------------------------------------------------- void GetLoadChoice(char &choice) {    cout << " Which of the following would you like to ship?" << " (W)olves" << " (S)teak" << " (R)abbits "; cout << "Enter W or S or R: "; cin >> choice; choice = toupper(choice);
while(choice != 'W' && choice != 'S' && choice != 'R') { cout << "Sorry, we don't ship that type of item. "; cout << "Which of the following would you like to add?" << " (W)olves" << " (S)teak" << " (R)abbits "; cin >> choice; choice = toupper(choice); } }
//--------------------------------------------------------------------------- // Name: AddLoad // Parameters: const char loadChoice - the type of item to be shipped // Returns: float cost // Purpose: Find the (positive integer) quantity of yards to be shipped, then // add the appropriate price to the total //--------------------------------------------------------------------------- float AddLoad(const char loadChoice) {    float cost = 0.0; int quantity = 0;    cout << "Enter the number of cubic yards you would like to ship: "; cin >> quantity; while(quantity < 1) { cout << "Sorry, you must enter a number >= 0. "; cout << "Enter the number of cubic yards would you like to ship: "; cin >> quantity; }    if (loadChoice == 'W') cost = cost + quantity * WOLF_PRICE; else if (loadChoice == 'S') cost = cost + quantity * STEAK_PRICE; else cost = cost + quantity * RABBIT_PRICE;
return cost; }
//--------------------------------------------------------------------------- // Name: PrintMain // Parameters: none // Returns: none // Purpose: Prints the main menu for the shipping scheduler //--------------------------------------------------------------------------- void PrintMain() { cout << "Welcome to the Aesop shipping LLC load scheduler " << "You can ship some combinations of rabbits, wolves, and frozen steak. " << "(Fares are all measured in dollars/cubic yard for convenience) " << "If you want to ship steak, you have to use a refrigerated train. " << "If you attempt to ship rabbits and wolves, the order will be cancelled. " << "If you attempt to ship non-frozen steak, the order will be cancelled. "; }
//--------------------------------------------------------------------------- // Name: RefrigeratedCar // Parameters: int &numRCars; add one to the number of refigerated cars used // Returns: 150 if the user requests a refrigerated car, 0 otherwise. // Purpose: Ask if the user wants a refrigerated car and return the additional // cost if so, 0 otherwise. //--------------------------------------------------------------------------- int RefrigeratedCar(int &numRCars) { char wantRefrigeratedCar; int cost = 0;
// If the user answers 'Y', increment the number of refrigerated cars used wantRefrigeratedCar = GetYorN("Do you require a refrigerated car? "); if (wantRefrigeratedCar == 'Y') { numRCars++; cost = 150; }
return cost; }
//--------------------------------------------------------------------------- // Name: IsValidOrder // Parameters: const char firstLoad, const char secondLoad, const int NumRCars // Returns: true if the order is valid, false otherwise // Purpose: check the loads to ensure that rabbits and wolves aren't // being shipped together, and ensure that the user ordered a // refrigerated car if they are shipping steak. If any violation // occured, tell the user which and return false. Otherwise, return true. //--------------------------------------------------------------------------- bool IsValidOrder(const char firstLoad, const char secondLoad, const int numRCars) { bool isValid = true;
// If there is anything wrong, change isValid to false if(firstLoad == 'W' && secondLoad == 'R') { cout << "Cannot ship both wolves and rabbits together "; isValid = false; } if(firstLoad == 'R' && secondLoad == 'W') { cout << "Cannot ship both wolves and rabbits together "; isValid = false; } if((firstLoad == 'S' || secondLoad == 'S') && (numRCars == 0)) { cout << "Steaks require a refrigerated car. "; isValid = false; } return isValid; }
//--------------------------------------------------------------------------- // Name: PrintBill // Parameters: const float totalCost - the cost of all the shipments // const int numRCars - the number of refigerator cars // Returns: void // Purpose: Prints the bill //--------------------------------------------------------------------------- void PrintBill(const float totalCost, const float numRCars) { cout << "Your total bill is $" << fixed << setprecision(2) << totalCost << ". " << "The price includes " << setprecision(0) << numRCars << " refrigerator cars. "; }
//--------------------------------------------------------------------------- // Name: YouAreFinished // Parameters: none // Returns: bool: true if finished, false if not finishted // Purpose: Prints the main menu for the shipping scheduler //--------------------------------------------------------------------------- bool YouAreFinished() { char choice;
choice = GetYorN(" Do you want to schedule another shipment? ");    // if the user entered 'N', then you are finished return (choice == 'N'); }
int main() { //shipment variables char firstLoad = ''; // type of the first load in a shipment char secondLoad = ''; // type of the second load in a shipment int shipmentRefrigCars = 0; // number of refrigerator cars for this shipment float shipmentRefrigCost = 0.0; // cost of the refrigerator cars for this shipment float shipmentCost = 0.0; // cost of this shipment
//total order variables int numRefrigCars = 0; // number of refrigerator cars total float totalCost = 0.0; // total cost of all shipments bool done = false; // whether or not the user is done shipping stuff
//Loop until the user is done
//initialize the shipmentRefrigCars, shipmentRefrigCost and shipmentCost variables
//Print the main menu    //Ask if the user needs a refrigerated car and store the number and cost of the //refrigerator car (if any) in the appropriate shipment variables //Find out what the user wants to in the first load    //Set the load cost to the cost of the first load
//Get the user's second choice of load
//Add the user's second load cost to the shipment cost    //If the order is valid, // add the shipment cost and the refrigerator car cost to the total cost // add the number of shipment refrigerator cars to the total number of refrigerator cars //else // inform the user that their shipment has been cancelled
//Ask the user whether or not they want to continue (Y or N)
//output the total cost and the number of refrigerator cars used
return 0; }
//Example program #include <iostream> #include <string> #include <iomanip> using namespace std;
const float WOLF_PRICE = 55.00; const float RABBIT_PRICE = 15.00; const float STEAK_PRICE = 12.00; //all of the above are measured in $/yd^3

//--------------------------------------------------------------------------- // Name: GetYorN // Parameters: Questions, string, input, the YN question to ask // Returns: char; the character the user entered. Must be 'y' or 'Y' or 'n' or 'N' // Purpose: This function returns the user's response to a yes/no question //--------------------------------------------------------------------------- char GetYorN(const string Question) { char choice;
cout << Question << " Enter Y or N: "; cin >> choice; choice = toupper(choice);
while (choice != 'Y' && choice != 'N') { cout << "Invalid character. Please enter either (Y) for yes or (N) for no. "; cout << Question << " Enter Y or N: "; cin >> choice; choice = toupper(choice); }
return (choice); }
//--------------------------------------------------------------------------- // Name: GetLoadChoice // Parameters: reference char choice // Returns: none (passed in via reference), Must be 'R', 'W', or 'S' // Purpose: Determine which of the items the user would like to ship // //--------------------------------------------------------------------------- void GetLoadChoice(char &choice) {    cout << " Which of the following would you like to ship?" << " (W)olves" << " (S)teak" << " (R)abbits "; cout << "Enter W or S or R: "; cin >> choice; choice = toupper(choice);
while(choice != 'W' && choice != 'S' && choice != 'R') { cout << "Sorry, we don't ship that type of item. "; cout << "Which of the following would you like to add?" << " (W)olves" << " (S)teak" << " (R)abbits "; cin >> choice; choice = toupper(choice); } }
//--------------------------------------------------------------------------- // Name: AddLoad // Parameters: const char loadChoice - the type of item to be shipped // Returns: float cost // Purpose: Find the (positive integer) quantity of yards to be shipped, then // add the appropriate price to the total //--------------------------------------------------------------------------- float AddLoad(const char loadChoice) {    float cost = 0.0; int quantity = 0;    cout << "Enter the number of cubic yards you would like to ship: "; cin >> quantity; while(quantity < 1) { cout << "Sorry, you must enter a number >= 0. "; cout << "Enter the number of cubic yards would you like to ship: "; cin >> quantity; }    if (loadChoice == 'W') cost = cost + quantity * WOLF_PRICE; else if (loadChoice == 'S') cost = cost + quantity * STEAK_PRICE; else cost = cost + quantity * RABBIT_PRICE;
return cost; }
//--------------------------------------------------------------------------- // Name: PrintMain // Parameters: none // Returns: none // Purpose: Prints the main menu for the shipping scheduler //--------------------------------------------------------------------------- void PrintMain() { cout << "Welcome to the Aesop shipping LLC load scheduler " << "You can ship some combinations of rabbits, wolves, and frozen steak. " << "(Fares are all measured in dollars/cubic yard for convenience) " << "If you want to ship steak, you have to use a refrigerated train. " << "If you attempt to ship rabbits and wolves, the order will be cancelled. " << "If you attempt to ship non-frozen steak, the order will be cancelled. "; }
//--------------------------------------------------------------------------- // Name: RefrigeratedCar // Parameters: int &numRCars; add one to the number of refigerated cars used // Returns: 150 if the user requests a refrigerated car, 0 otherwise. // Purpose: Ask if the user wants a refrigerated car and return the additional // cost if so, 0 otherwise. //--------------------------------------------------------------------------- int RefrigeratedCar(int &numRCars) { char wantRefrigeratedCar; int cost = 0;
// If the user answers 'Y', increment the number of refrigerated cars used wantRefrigeratedCar = GetYorN("Do you require a refrigerated car? "); if (wantRefrigeratedCar == 'Y') { numRCars++; cost = 150; }
return cost; }
//--------------------------------------------------------------------------- // Name: IsValidOrder // Parameters: const char firstLoad, const char secondLoad, const int NumRCars // Returns: true if the order is valid, false otherwise // Purpose: check the loads to ensure that rabbits and wolves aren't // being shipped together, and ensure that the user ordered a // refrigerated car if they are shipping steak. If any violation // occured, tell the user which and return false. Otherwise, return true. //--------------------------------------------------------------------------- bool IsValidOrder(const char firstLoad, const char secondLoad, const int numRCars) { bool isValid = true;
// If there is anything wrong, change isValid to false if(firstLoad == 'W' && secondLoad == 'R') { cout << "Cannot ship both wolves and rabbits together "; isValid = false; } if(firstLoad == 'R' && secondLoad == 'W') { cout << "Cannot ship both wolves and rabbits together "; isValid = false; } if((firstLoad == 'S' || secondLoad == 'S') && (numRCars == 0)) { cout << "Steaks require a refrigerated car. "; isValid = false; } return isValid; }
//--------------------------------------------------------------------------- // Name: PrintBill // Parameters: const float totalCost - the cost of all the shipments // const int numRCars - the number of refigerator cars // Returns: void // Purpose: Prints the bill //--------------------------------------------------------------------------- void PrintBill(const float totalCost, const float numRCars) { cout << "Your total bill is $" << fixed << setprecision(2) << totalCost << ". " << "The price includes " << setprecision(0) << numRCars << " refrigerator cars. "; }
//--------------------------------------------------------------------------- // Name: YouAreFinished // Parameters: none // Returns: bool: true if finished, false if not finishted // Purpose: Prints the main menu for the shipping scheduler //--------------------------------------------------------------------------- bool YouAreFinished() { char choice;
choice = GetYorN(" Do you want to schedule another shipment? ");    // if the user entered 'N', then you are finished return (choice == 'N'); }
int main() { //shipment variables char firstLoad = ''; // type of the first load in a shipment char secondLoad = ''; // type of the second load in a shipment int shipmentRefrigCars = 0; // number of refrigerator cars for this shipment float shipmentRefrigCost = 0.0; // cost of the refrigerator cars for this shipment float shipmentCost = 0.0; // cost of this shipment
//total order variables int numRefrigCars = 0; // number of refrigerator cars total float totalCost = 0.0; // total cost of all shipments bool done = false; // whether or not the user is done shipping stuff
//Loop until the user is done
//initialize the shipmentRefrigCars, shipmentRefrigCost and shipmentCost variables
//Print the main menu    //Ask if the user needs a refrigerated car and store the number and cost of the //refrigerator car (if any) in the appropriate shipment variables //Find out what the user wants to in the first load    //Set the load cost to the cost of the first load
//Get the user's second choice of load
//Add the user's second load cost to the shipment cost    //If the order is valid, // add the shipment cost and the refrigerator car cost to the total cost // add the number of shipment refrigerator cars to the total number of refrigerator cars //else // inform the user that their shipment has been cancelled
//Ask the user whether or not they want to continue (Y or N)
//output the total cost and the number of refrigerator cars used
return 0; }
1. Problem Statement: The objective of this programming assignment is to become familiar with calling C++ functions. You will be modifying an existing program: hw4a.cpp. Your task is to write a main program to accomplish certain goals by calling functions that are already written and provided to you Aesop Shipping LLC is a freight company that schedules loads of three commodities live arctic wolves, live arctic rabbits, and frozen steak. Due to the nature of the shipments, some restrictions are in place Orders consist of 1 or more shipments Each shipment consists of a pair of loads . Each load consists of one of the three commodities Live wolves and rabbits cannot be shipped in the same shipment, for obvious reasons Steak requires refrigerated cars (the animals won't mind, they're arctic). Your task is to use the provided functions to write a main function that will prompt the user for the information you will need to calculate the total bill. The variables, and functions are already provided. Your task is to get what and in what quantity the user wants to ship, ensure that all restrictions are not being violated, and print the total cost of the shipment if it is valid. prices, necessary 2. Design Once you have an idea of what needs to be done, think about how you can use the provided functions in your main) function, which actual parameters need to be passed to the functions, the type of parameters that are being used and how you can use the return values A skeleton of the main program is included with comments showing the necessary steps where work needs to be provided. The already declared, therefore all you need to do is use them in your work. necessary variables for your program are Do not change the functions themselves.

Explanation / Answer

Here is the main program

/**************/

//Example program
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const float WOLF_PRICE = 55.00;
const float RABBIT_PRICE = 15.00;
const float STEAK_PRICE = 12.00;
//all of the above are measured in $/yd^3


//---------------------------------------------------------------------------
// Name: GetYorN
// Parameters: Questions, string, input, the YN question to ask
// Returns: char; the character the user entered. Must be 'y' or 'Y' or 'n' or 'N'
// Purpose: This function returns the user's response to a yes/no question
//---------------------------------------------------------------------------
char GetYorN(const string Question)
{
char choice;

   cout << Question << " Enter Y or N: ";
   cin >> choice;
   choice = toupper(choice);

   while (choice != 'Y' && choice != 'N')
   {
      cout << "Invalid character. Please enter either (Y) for yes or (N) for no. ";
      cout << Question << " Enter Y or N: ";
      cin >> choice;
      choice = toupper(choice);
   }

   return (choice);
}

//---------------------------------------------------------------------------
// Name: GetLoadChoice
// Parameters: reference char choice
// Returns: none (passed in via reference), Must be 'R', 'W', or 'S'
// Purpose: Determine which of the items the user would like to ship
//
//---------------------------------------------------------------------------
void GetLoadChoice(char &choice)
{
    cout << " Which of the following would you like to ship?"
         << " (W)olves"
         << " (S)teak"
         << " (R)abbits ";
    cout << "Enter W or S or R: ";
    cin >> choice;
    choice = toupper(choice);

    while(choice != 'W' && choice != 'S' && choice != 'R')
    {
        cout << "Sorry, we don't ship that type of item. ";
        cout << "Which of the following would you like to add?" <<
                " (W)olves" <<
                " (S)teak" <<
                " (R)abbits ";
        cin >> choice;
        choice = toupper(choice);
    }
}

//---------------------------------------------------------------------------
// Name: AddLoad
// Parameters: const char loadChoice - the type of item to be shipped
// Returns: float cost
// Purpose: Find the (positive integer) quantity of yards to be shipped, then
//          add the appropriate price to the total
//---------------------------------------------------------------------------
float AddLoad(const char loadChoice)
{
float cost = 0.0;
int quantity = 0;

    cout << "Enter the number of cubic yards you would like to ship: ";
    cin >> quantity;
    while(quantity < 1)
    {
        cout << "Sorry, you must enter a number >= 0. ";
        cout << "Enter the number of cubic yards would you like to ship: ";
        cin >> quantity;
    }

    if (loadChoice == 'W')
       cost = cost + quantity * WOLF_PRICE;
    else if (loadChoice == 'S')
       cost = cost + quantity * STEAK_PRICE;
    else
       cost = cost + quantity * RABBIT_PRICE;

    return cost;
}

//---------------------------------------------------------------------------
// Name: PrintMain
// Parameters: none
// Returns: none
// Purpose: Prints the main menu for the shipping scheduler
//---------------------------------------------------------------------------
void PrintMain()
{
    cout << "Welcome to the Aesop shipping LLC load scheduler "
         << "You can ship some combinations of rabbits, wolves, and frozen steak. "
         << "(Fares are all measured in dollars/cubic yard for convenience) "
         << "If you want to ship steak, you have to use a refrigerated train. "
         << "If you attempt to ship rabbits and wolves, the order will be cancelled. "
         << "If you attempt to ship non-frozen steak, the order will be cancelled. ";
}

//---------------------------------------------------------------------------
// Name: RefrigeratedCar
// Parameters: int &numRCars; add one to the number of refigerated cars used
// Returns: 150 if the user requests a refrigerated car, 0 otherwise.
// Purpose: Ask if the user wants a refrigerated car and return the additional
//          cost if so, 0 otherwise.
//---------------------------------------------------------------------------
int RefrigeratedCar(int &numRCars)
{
char wantRefrigeratedCar;
int cost = 0;

    // If the user answers 'Y', increment the number of refrigerated cars used
    wantRefrigeratedCar = GetYorN("Do you require a refrigerated car? ");
    if (wantRefrigeratedCar == 'Y')
    {
       numRCars++;
       cost = 150;
    }

    return cost;
}

//---------------------------------------------------------------------------
// Name: IsValidOrder
// Parameters: const char firstLoad, const char secondLoad, const int NumRCars
// Returns: true if the order is valid, false otherwise
// Purpose: check the loads to ensure that rabbits and wolves aren't
//          being shipped together, and ensure that the user ordered a
//          refrigerated car if they are shipping steak. If any violation
//          occured, tell the user which and return false. Otherwise, return true.
//---------------------------------------------------------------------------
bool IsValidOrder(const char firstLoad, const char secondLoad, const int numRCars)
{
bool isValid = true;

    // If there is anything wrong, change isValid to false
    if(firstLoad == 'W' && secondLoad == 'R')
    {
        cout << "Cannot ship both wolves and rabbits together ";
        isValid = false;
    }
    if(firstLoad == 'R' && secondLoad == 'W')
    {
        cout << "Cannot ship both wolves and rabbits together ";
        isValid = false;
    }
    if((firstLoad == 'S' || secondLoad == 'S') && (numRCars == 0))
    {
        cout << "Steaks require a refrigerated car. ";
        isValid = false;
    }

return isValid;
}

//---------------------------------------------------------------------------
// Name: PrintBill
// Parameters: const float totalCost - the cost of all the shipments
//             const int numRCars - the number of refigerator cars
// Returns: void
// Purpose: Prints the bill
//---------------------------------------------------------------------------
void PrintBill(const float totalCost, const float numRCars)
{
    cout << "Your total bill is $"
         << fixed << setprecision(2) << totalCost << ". "
         << "The price includes " << setprecision(0) << numRCars << " refrigerator cars. ";
}

//---------------------------------------------------------------------------
// Name: YouAreFinished
// Parameters: none
// Returns: bool: true if finished, false if not finishted
// Purpose: Prints the main menu for the shipping scheduler
//---------------------------------------------------------------------------
bool YouAreFinished()
{
char choice;

    choice = GetYorN(" Do you want to schedule another shipment? ");

    // if the user entered 'N', then you are finished
    return (choice == 'N');
}

int main()
{
    //shipment variables
    char firstLoad = '';    // type of the first load in a shipment
    char secondLoad = '';   // type of the second load in a shipment
    int shipmentRefrigCars = 0; // number of refrigerator cars for this shipment
    float shipmentRefrigCost = 0.0;      // cost of the refrigerator cars for this shipment
    float shipmentCost = 0.0;    // cost of this shipment

    //total order variables
    int numRefrigCars = 0;       // number of refrigerator cars total
    float totalCost = 0.0;       // total cost of all shipments
    bool done = false;           // whether or not the user is done shipping stuff

    //Loop until the user is done
    do
    {
       shipmentRefrigCars=0;
       shipmentRefrigCost=0.0;
       shipmentCost=0.0;
        //initialize the shipmentRefrigCars, shipmentRefrigCost and shipmentCost variables

        //Print the main menu
       PrintMain();

        //Ask if the user needs a refrigerated car and store the number and cost of the
        //refrigerator car (if any) in the appropriate shipment variables
       shipmentRefrigCost=RefrigeratedCar(shipmentRefrigCars);

        //Find out what the user wants to in the first load
       GetLoadChoice(firstLoad);

        //Set the load cost to the cost of the first load
       shipmentCost=AddLoad(firstLoad);

        //Get the user's second choice of load
       GetLoadChoice(secondLoad);

        //Add the user's second load cost to the shipment cost
       shipmentCost+=AddLoad(secondLoad);

        //If the order is valid,
        //   add the shipment cost and the refrigerator car cost to the total cost
        //   add the number of shipment refrigerator cars to the total number of refrigerator cars
        //else
        //   inform the user that their shipment has been cancelled
       if(IsValidOrder(firstLoad,secondLoad,shipmentRefrigCars)){
           totalCost=totalCost+shipmentCost+shipmentRefrigCost;
           numRefrigCars=numRefrigCars+shipmentRefrigCars;
       }
       else{
           cout<<"Your Shipment has been canceled "<<endl;
       }


        //Ask the user whether or not they want to continue (Y or N)
    }while(!YouAreFinished());
    //output the total cost and the number of refrigerator cars used
    PrintBill(totalCost,numRefrigCars);
    return 0;
}


/***************/output

Welcome to the Aesop shipping LLC load scheduler
You can ship some combinations of rabbits, wolves, and frozen steak.
(Fares are all measured in dollars/cubic yard for convenience)
If you want to ship steak, you have to use a refrigerated train.
If you attempt to ship rabbits and wolves, the order will be cancelled.
If you attempt to ship non-frozen steak, the order will be cancelled.

Do you require a refrigerated car?    Enter Y or N: Y

Which of the following would you like to ship?
(W)olves
(S)teak
(R)abbits
Enter W or S or R: W
Enter the number of cubic yards you would like to ship: 2

Which of the following would you like to ship?
(W)olves
(S)teak
(R)abbits
Enter W or S or R: S
Enter the number of cubic yards you would like to ship: 4

Do you want to schedule another shipment?    Enter Y or N: Y
Welcome to the Aesop shipping LLC load scheduler
You can ship some combinations of rabbits, wolves, and frozen steak.
(Fares are all measured in dollars/cubic yard for convenience)
If you want to ship steak, you have to use a refrigerated train.
If you attempt to ship rabbits and wolves, the order will be cancelled.
If you attempt to ship non-frozen steak, the order will be cancelled.

Do you require a refrigerated car?    Enter Y or N: N

Which of the following would you like to ship?
(W)olves
(S)teak
(R)abbits
Enter W or S or R: W
Enter the number of cubic yards you would like to ship: 6

Which of the following would you like to ship?
(W)olves
(S)teak
(R)abbits
Enter W or S or R: S
Enter the number of cubic yards you would like to ship: 6
Steaks require a refrigerated car.
Your Shipment has been canceled

Do you want to schedule another shipment?    Enter Y or N: N
Your total bill is $308.00. The price includes 1 refrigerator cars.