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

I need help with this program which is Functions //-----------------------------

ID: 3551894 • Letter: I

Question

I need help with this program which is Functions //--------------------------------------------------------------------------- // Program: hw4.cpp // Purpose: This program simulates purchasing Walton Arts Center event tickets // Comments: This program uses the "toupper" function which translates // all lowercase input letters to their corresponding // uppercase equivalent. All other input values are // returned unchanged. //--------------------------------------------------------------------------- #include #include using namespace std; const float BALCONY_PRICE = 25.00; const float ORCHESTRA_PRICE = 50.00; const float BOX_PRICE = 75.00; const float PREMIUM_PRICE = 20.00; const float PARKING_PRICE_PER_DAY = 10.00; const int MAX_DAYS_ALLOWED = 5; //--------------------------------------------------------------------------- // 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: ParkingPass // Parameters: none // Returns: float; the cost of the pass ordered // Purpose: This function returns the cost of parking pass that a participant wants //--------------------------------------------------------------------------- float ParkingPass() { char ParkingPassChoice = ''; int ParkingDays = 0; float ParkingPassPrice = 0.0; ParkingPassChoice = GetYorN ("Would you like to buy parking passes?"); if (ParkingPassChoice == 'Y') { cout << "Parking passes are $10 per day." << endl; cout << "How many days of parking would you like to buy (1-" << MAX_DAYS_ALLOWED << "): "; cin >> ParkingDays; while(ParkingDays < 1 || ParkingDays > MAX_DAYS_ALLOWED) { cout << "Please enter a valid number of days (1-" << MAX_DAYS_ALLOWED << ") or enter 0 to cancel your purchase: "; cin >> ParkingDays; } ParkingPassPrice = PARKING_PRICE_PER_DAY * ParkingDays; } return ParkingPassPrice; } //--------------------------------------------------------------------------- // Name: CalculatePrice // Parameters: TicketName: string, pass by value, name of ticket type // PricePerTicket: float, pass by value, cost of one ticket // NumTickets, int, input, number of tickets to purchase. // Returns: float - the total cost of the tickets purchased // Purpose: This function calculates and returns the cost of the tickets; // It also prints out that cost to the user. //--------------------------------------------------------------------------- float CalculatePrice(const string TicketName, const float PricePerTicket, const float NumTickets) { float Amount; Amount = NumTickets * PricePerTicket; cout << " -------------------------------------------------------------------------- "; cout << "You have chosen to order " << noshowpoint << setprecision(0) << NumTickets << " " << TicketName << " tickets. "; cout << fixed << showpoint << setprecision(2); cout << "Each ticket costs $" << PricePerTicket << ". "; cout << "The price for these tickets is $" << Amount << endl; cout << "-------------------------------------------------------------------------- "; return Amount; } //--------------------------------------------------------------------------- // Name: AddPremium // Parameters: Price, float, input/output, current cost of the tickets // It gets updated if the user selects the premium upgrade // NumTickets, const int, input, the number of tickets. // Returns: none // Purpose: This function adds the cost of the premium upgrade, if the patron // chooses to purchase it //--------------------------------------------------------------------------- void AddPremium(float &Price, const int NumTickets) { char Choice; cout << " ---------------------------------------------------------------------------------------------- "; cout << "You have chosen to order Box seat tickets. "; cout << "You can add a premium package that includes a souvenier and free refreshments. " << "This costs $" << PREMIUM_PRICE<< " per ticket. "; cout << "---------------------------------------------------------------------------------------------- "; Choice = GetYorN("Would you like to add the premium package?"); if (Choice == 'Y') { Price = Price + NumTickets * PREMIUM_PRICE; cout << "Your Box seats with the premium upgrade cost $" << fixed << showpoint << setprecision(2) << Price << " for " << noshowpoint << setprecision (0) << NumTickets << " tickets. "; } } //--------------------------------------------------------------------------- // Name: MainMenu // Parameters: none // Returns: none // Purpose: This function prints the main menu describing various tickets // offered by the Walton Arts Center //--------------------------------------------------------------------------- void MainMenu() { char MoreInfo; cout << "The Walton Arts Center offers its patrons an assortment " << "of tickets to fit different budgets. " << "There are three kinds of seats that the Arts Center offers: " << "Balcony, Orchestra, and Box seats. "; MoreInfo = GetYorN("Would you like more information about each ticket type?"); if(MoreInfo == 'Y') { cout << "The Balcony tickets are the cheapest, because the tickets" << " are located the furthest from stage. "; cout << "The Orchestra tickets are more expensive, because the tickets allow " << "participants to sit closer to the stage. "; cout << "The Box seat tickets are the most expensive, " << "because the seats are very comfortable and private. " << "Box seat purchasers can also add other options (souvenirs and refreshments)" << " to their purchase. "; } } //--------------------------------------------------------------------------- // Name: TicketType // Parameters: none // Returns: char; the selection of ticket to be purchased // Purpose: This function asks the user which ticket they would like to buy //--------------------------------------------------------------------------- char GetTicketType() { char TicketType; cout << " Which ticket would you like to purchase next? "; cout << "Type B for Balcony, O for Orchestra, or X for Box: "; cin >> TicketType; TicketType = toupper(TicketType); //convert to uppercase //User I/O error-checking while(TicketType != 'B' && TicketType != 'O' && TicketType != 'X') { cout << TicketType << " is invalid input. "; cout << "Type B for Balcony, O for Orchestra, or X for Box: "; cin >> TicketType; TicketType = toupper(TicketType); } return TicketType; } //--------------------------------------------------------------------------- // Name: GetNumTickets // Parameters: NumTickets, integer, reference, passes back the number of tickets requested // Returns: none // Purpose: This function asks the user how many tickets would like to buy //--------------------------------------------------------------------------- void GetNumTickets (int &NumTickets) { cout << "Enter the number of tickets you would like to buy: "; cin >> NumTickets; while(NumTickets < 0) //User I/O error-checking { cout << "Please enter a non-negative number of tickets: "; cin >> NumTickets; } } //--------------------------------------------------------------------------- // This is the main program that you need to write //--------------------------------------------------------------------------- int main () { // Variable Declarations char Choice = ''; // what the user enters int NumTickets = 0; // how many tickets they want to buy float Price = 0.0; // the price of one set of tickets float Total = 0.0; // the total price of all tickets // Print your name and UAID // Loop until the user is done // Print the main menu describing the tickets offered // Ask the user type what ticket they want to purchase next // Find out how many tickets they want // Find out the price of the tickets // If the user selects Box seats, ask if they want the premium package // Add the ticket price to a running total // Ask if they want to continue (Y or N) // When the loop is done // Sell the user parking pass // Print out the total amount of all the tickets and parking passes // that the participant has purchased to 2 decimal places, with a $ return 0; }

Explanation / Answer

Updated program below

Added required include lines and all of the main()

Successfully compiles/runs in Visual Studio 2013

YOU STILL NEED TO UPDATE THE LINES THAT PRINT

YOUR NAME and UAID



//---------------------------------------------------------------------------
// Program: hw4.cpp
// Purpose: This program simulates purchasing Walton Arts Center event tickets
// Comments: This program uses the "toupper" function which translates
//           all lowercase input letters to their corresponding
//           uppercase equivalent. All other input values are
//           returned unchanged.
//---------------------------------------------------------------------------
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
const float BALCONY_PRICE = 25.00;
const float ORCHESTRA_PRICE = 50.00;
const float BOX_PRICE = 75.00;
const float PREMIUM_PRICE = 20.00;
const float PARKING_PRICE_PER_DAY = 10.00;
const int MAX_DAYS_ALLOWED = 5;
//---------------------------------------------------------------------------
// 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: ParkingPass
// Parameters: none
// Returns: float; the cost of the pass ordered
// Purpose: This function returns the cost of parking pass that a participant wants //---------------------------------------------------------------------------
float ParkingPass() {
    char ParkingPassChoice = '';
    int ParkingDays = 0;
    float ParkingPassPrice = 0.0;
    ParkingPassChoice = GetYorN("Would you like to buy parking passes?");
    if (ParkingPassChoice == 'Y') {
        cout << "Parking passes are $10 per day." << endl;
        cout << "How many days of parking would you like to buy (1-" << MAX_DAYS_ALLOWED << "): ";
        cin >> ParkingDays;
        while (ParkingDays < 1 || ParkingDays > MAX_DAYS_ALLOWED) {
            cout << "Please enter a valid number of days (1-" << MAX_DAYS_ALLOWED << ") or enter 0 to cancel your purchase: ";
            cin >> ParkingDays;
        }
        ParkingPassPrice = PARKING_PRICE_PER_DAY * ParkingDays;
    }
    return ParkingPassPrice;
}
//---------------------------------------------------------------------------
// Name: CalculatePrice
// Parameters: TicketName: string, pass by value, name of ticket type
// PricePerTicket: float, pass by value, cost of one ticket
// NumTickets, int, input, number of tickets to purchase.
// Returns: float - the total cost of the tickets purchased
// Purpose: This function calculates and returns the cost of the tickets;

// It also prints out that cost to the user.
//---------------------------------------------------------------------------
float CalculatePrice(const string TicketName, const float PricePerTicket, const float NumTickets) {
    float Amount;
    Amount = NumTickets * PricePerTicket;
    cout << " -------------------------------------------------------------------------- ";
    cout << "You have chosen to order " << noshowpoint << setprecision(0) << NumTickets << " " << TicketName << " tickets. ";
    cout << fixed << showpoint << setprecision(2);
    cout << "Each ticket costs $" << PricePerTicket << ". ";
    cout << "The price for these tickets is $" << Amount << endl;
    cout << "-------------------------------------------------------------------------- ";
    return Amount;
}
//---------------------------------------------------------------------------
// Name: AddPremium
// Parameters: Price, float, input/output, current cost of the tickets
// It gets updated if the user selects the premium upgrade
// NumTickets, const int, input, the number of tickets.
// Returns: none
// Purpose: This function adds the cost of the premium upgrade, if the patron
// chooses to purchase it
//---------------------------------------------------------------------------
void AddPremium(float &Price, const int NumTickets) {
    char Choice;
    cout << " ---------------------------------------------------------------------------------------------- ";
    cout << "You have chosen to order Box seat tickets. ";
    cout << "You can add a premium package that includes a souvenier and free refreshments. " << "This costs $" << PREMIUM_PRICE << " per ticket. ";
    cout << "---------------------------------------------------------------------------------------------- ";
    Choice = GetYorN("Would you like to add the premium package?");
    if (Choice == 'Y') {
        Price = Price + NumTickets * PREMIUM_PRICE;
        cout << "Your Box seats with the premium upgrade cost $" << fixed << showpoint << setprecision(2) << Price << " for " << noshowpoint << setprecision(0) << NumTickets << " tickets. ";
    }
}
//---------------------------------------------------------------------------
// Name: MainMenu
// Parameters: none
// Returns: none
// Purpose: This function prints the main menu describing various tickets
// offered by the Walton Arts Center
//---------------------------------------------------------------------------
void MainMenu() {
    char MoreInfo;
    cout << "The Walton Arts Center offers its patrons an assortment " << "of tickets to fit different budgets. " << "There are three kinds of seats that the Arts Center offers: " << "Balcony, Orchestra, and Box seats. ";
    MoreInfo = GetYorN("Would you like more information about each ticket type?");
    if (MoreInfo == 'Y') {
        cout << "The Balcony tickets are the cheapest, because the tickets" << " are located the furthest from stage. ";
        cout << "The Orchestra tickets are more expensive, because the tickets allow " << "participants to sit closer to the stage. ";
        cout << "The Box seat tickets are the most expensive, " << "because the seats are very comfortable and private. " << "Box seat purchasers can also add other options (souvenirs and refreshments)" << " to their purchase. ";
    }
}
//---------------------------------------------------------------------------
// Name: TicketType
// Parameters: none
// Returns: char; the selection of ticket to be purchased
// Purpose: This function asks the user which ticket they would like to buy //---------------------------------------------------------------------------
char GetTicketType() {
    char TicketType;
    cout << " Which ticket would you like to purchase next? ";
    cout << "Type B for Balcony, O for Orchestra, or X for Box: ";
    cin >> TicketType;
    TicketType = toupper(TicketType); //convert to uppercase
    //User I/O error-checking
    while (TicketType != 'B' && TicketType != 'O' && TicketType != 'X') {
        cout << TicketType << " is invalid input. ";
        cout << "Type B for Balcony, O for Orchestra, or X for Box: ";
        cin >> TicketType;
        TicketType = toupper(TicketType);
    }
    return TicketType;
}
//---------------------------------------------------------------------------
// Name: GetNumTickets
// Parameters: NumTickets, integer, reference, passes back the number of tickets requested
// Returns: none
// Purpose: This function asks the user how many tickets would like to buy //---------------------------------------------------------------------------
void GetNumTickets(int &NumTickets) {
    cout << "Enter the number of tickets you would like to buy: ";
    cin >> NumTickets;
    while (NumTickets < 0) { //User I/O error-checking
        cout << "Please enter a non-negative number of tickets: ";
        cin >> NumTickets;
    }
}
//---------------------------------------------------------------------------
// This is the main program that you need to write
//---------------------------------------------------------------------------
int main() {
    // Variable Declarations
    char Choice = ''; // what the user enters
    int NumTickets = 0; // how many tickets they want to buy
    float Price = 0.0; // the price of one set of tickets
    float Total = 0.0; // the total price of all tickets
    // Print your name and UAID
    cout << "YOUR NAME ";
    cout << "YOUR UAID ";
    // Loop until the user is done
    while (!(Choice == 'N')) {
        MainMenu(); // Print the main menu describing the tickets offered
        Choice = GetTicketType(); // Ask the user type what ticket they want to purchase next
        GetNumTickets(NumTickets);// Find out how many tickets they want
        if (Choice == 'B') {
            Price = CalculatePrice("Balcony", BALCONY_PRICE, NumTickets); // Find out the price of the tickets
        }
        else if (Choice == 'O') {
            Price = CalculatePrice("Orchestra", ORCHESTRA_PRICE, NumTickets);
        }
        else {
            Price = CalculatePrice("Box", BOX_PRICE, NumTickets);
        }
        if (Choice == 'X') { // If the user selects Box seats, ask if they want the premium package
            AddPremium(Price, NumTickets);
        }
        Total = Total + Price; // Add the ticket price to a running total
        Choice = GetYorN("Purchase More Tickets?"); // Ask if they want to continue (Y or N)
    }
    // When the loop is done
    Price = ParkingPass(); // Sell the user parking pass
    Total = Total + Price; // Print out the total amount of all the tickets and parking passes
    cout << "Total = $" << fixed << setprecision(2) << Total << endl;// that the participant has purchased to 2 decimal places, with a $
    return 0;
}