1 Replies - 452 Views - Last Post: 05 October 2010 - 10:14 PM Rate Topic: -----

#1 vizirus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-September 10

Homework with functions

Posted 05 October 2010 - 09:59 PM

Ok well I am a complete beginner to programming and i had to take a programming class this year so please take it easy on me. For my homework i was given a program with functions and i have to use the functions in the main() function at the very end. Ive already filled in some spaces but now im stuck on the main()function. Im not asking for the whole thing but Im not sure which variables to declare or if i even have to declare more. Also the loop until user is done and the reset variable values comments are throwing me off. I should be able to do the rest on my own after i figure those out. Thanks in advance!

//---------------------------------------------------------------------------
// Program:  hw3a.cpp
// Purpose:  This program simulates purchasing University parking permits
// 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 <iostream>
#include <iomanip>
using namespace std;

const float UPGRADE_COST = 69.75;

//---------------------------------------------------------------------------
// Name: GameDayPass
// Parameters: none
// Returns: float; the cost of the permits ordered
// Purpose: This function returns the cost of football game parking permits
//          that a student wants
//---------------------------------------------------------------------------
float GameDayPass()
{
    const float PRICE_PER_DAY = 40.0;
    const int MAX_HOME_GAMES = 5;

    char GameDayChoice = '\0';
    float GameDays = 0.0;
    float GameDayPrice = 0.0;

    do
    {
        cout << "Would you like to buy 2010 Razorback Football parking passes: ";
        cin >> GameDayChoice;
        GameDayChoice = toupper(GameDayChoice);

        switch(GameDayChoice)
        {
        case 'Y':
            cout << "Football parking passes are $40 per day." << endl;
            cout << "How many days of football parking would you like to buy (Choose 1-5): ";
            cin >> GameDays;
            while(GameDays < 0 || GameDays > MAX_HOME_GAMES)
            {
                cout << "Please select a valid number of days (Choose 1-5), or choose 0 to cancel your purchase: ";
                cin >> GameDays;
            }
            GameDayPrice = PRICE_PER_DAY * GameDays;
            break;
        case 'N':
            //we will do nothing if the student doesn't want football parking
            break;

        default:
            cout << "Unrecognized selection, please select either (Y) for yes or (N) for no.\n";
            break;
        }

    }while(GameDayChoice != 'Y' && GameDayChoice != 'N');

    return GameDayPrice;
}

//---------------------------------------------------------------------------
// Name: RemotePermit
// Parameters: Amount, float, output, passes back the cost of the permit
//             Months, int, input, passes in the the number of months
//                                   requested by the user
// Returns: nothing
// Purpose: This function calculates the amount of money that the student needs
//          to pay if he/she wants a Remote Parking permit

void RemotePermit(float &Amount, const int Months)

{
    const float PRICE_PER_MONTH = 16.11;

    char Upgrade = '\0';

    cout << "\n----------------------------------------------------------------------------------------------\n";
    cout << "You have chosen to order a Remote Parking permit.\n";
    cout << "Each month of this type costs " << PRICE_PER_MONTH <<  ".\n";
    cout << "You will need to specify whether you would like to upgrade to a Student permit. \n";
    cout << "Access to parking lots labeled \"STUDENT\" costs a one-time fee of $"
         << UPGRADE_COST << ".\n";
    cout << "----------------------------------------------------------------------------------------------\n\n";

    cout << "Would you like to upgrade to a Student permit: ";
    cin >> Upgrade;
    Upgrade = toupper(Upgrade);

    while(Upgrade != 'Y' && Upgrade != 'N')
    {
        cout << "Unrecognized selection, please select either (Y) for yes or (N) for no.\n";
        cin >> Upgrade;
    }

    Amount = PRICE_PER_MONTH * Months;
    if(Upgrade == 'Y')
        Amount = Amount + UPGRADE_COST;

    cout << "The Remote Permit will cost you $"
         << fixed << showpoint << setprecision(2) << Amount
         << " for " << Months << " months.\n";
}

//---------------------------------------------------------------------------
// Name: ResidentPermit

// Parameters: Amount, float, output, passes back the cost of the permit
//             Months, int, input, passes in the the number of months
//                                   requested by the user
// Returns: nothing
// Purpose: This function calculates the amount of money that the student needs
//          to pay if he/she wants a Resident Parking permit
//---------------------------------------------------------------------------
void ResidentPermit(float &Amount, const int Months)
{
    const float PRICE_PER_MONTH = 39.91;

    char Upgrade = '\0';

    cout << "\n----------------------------------------------------------------------------------------------\n";
    cout << "You have chosen to order a Resident Parking permit.\n";
    cout << "Each month of this type costs " << PRICE_PER_MONTH <<  ".\n";
    cout << "You will need to specify whether you would like to upgrade to a Resident Reserved permit. \n";
    cout << "Access to parking lots labeled \"RESIDENT RESERVED\" costs a one-time fee of $"

         << 2*UPGRADE_COST << ".\n";
    cout << "----------------------------------------------------------------------------------------------\n\n";

    cout << "Would you like to upgrade to a Resident Reserved permit: ";
    cin >> Upgrade;
    Upgrade = toupper(Upgrade);

    while(Upgrade != 'Y' && Upgrade != 'N')
    {
        cout << "Unrecognized selection, please select either (Y) for yes or (N) for no.\n";
        cin >> Upgrade;
    }

    Amount = PRICE_PER_MONTH * Months;
    if(Upgrade == 'Y')
        Amount = Amount + 2*UPGRADE_COST;

    cout << "The Resident Permit will cost you $"

         << fixed << showpoint << setprecision(2) << Amount
         << " for " << Months << " months.\n";
}

//---------------------------------------------------------------------------
// Name: GaragePermit
// Parameters: Amount, float, output, passes back the cost of the permit
// Returns: none
// Purpose: This function calculates the amount of money the student needs
//          to pay if he/she wants a Garage permit
//---------------------------------------------------------------------------
void GaragePermit(float &Amount)
{
    const float ANNUAL_HARMON = 652.84;
    const float ANNUAL_GARLAND = 792.46;

    char GarageChoice = '\0';

    cout << "\n----------------------------------------------------------------------------------------------\n";
    cout << "You have chosen to order a Garage parking permit.\n";
    cout << "There are two types of Garage permits: a Harmon Garage permit and a Garland Garage permit.\n";
    cout << "Garage permits are sold as annual permits only.\n";
    cout << "A one-year permit to park in the Harmon Garage costs " << ANNUAL_HARMON << ".\n";
    cout << "A one-year permit to park in the Garland Garage costs " << ANNUAL_GARLAND << ".\n";
    cout << "You will need to specify for which garage you want to purchase a permit.\n";
    cout << "----------------------------------------------------------------------------------------------\n\n";

    cout << "Which garage would you like to park at? Please select either (H) for Harmon or (G) for Garland.\n";
    cin >> GarageChoice;
    GarageChoice = toupper(GarageChoice);

    while(GarageChoice != 'H' && GarageChoice != 'G')
    {
        cout << "Unrecognized selection, please select either (H) for Harmon or (G) for Garland.\n";
        cin >> GarageChoice; 
    }


    if(GarageChoice == 'H')
        Amount = ANNUAL_HARMON;
    else
        Amount = ANNUAL_GARLAND;

    cout << "The Garage permit will cost you $"
         << fixed << showpoint << setprecision(2) << Amount
         << " for one year.\n";
}

//---------------------------------------------------------------------------
// Name: MainMenu
// Parameters: none
// Returns: none
// Purpose: This function prints the main menu describing various permits
//          offered by the University of Arkansas
//---------------------------------------------------------------------------

void MainMenu()
{
    char MoreInfo;

    cout << "The University of Arkansas offers its students an assortment\n"
         << "of on-campus parking permits to fit different needs and budgets.\n"
         << "There are three main permits that the university offers:\n"
         << "a Remote Permit, a Resident Permit, and a Garage Permit.\n\n";

    cout << "Would you like more information about each permit type:";
    cin >> MoreInfo;
    MoreInfo = toupper(MoreInfo);

    while(MoreInfo != 'Y' && MoreInfo != 'N')
    {
        cout << "Unrecognized selection, please select either (Y) to see more information "
             << "or (N) to skip the additional information.\n";
        cin >> MoreInfo;

    }

    if(MoreInfo == 'Y')
    {
        cout << "The Remote Permits are the cheapest, because the parking lots "
             << "are generally located the furthest from buildings on campus.\n"
             << "You can purchase this permit for any number of months, and you "
             << "may then park in any lots designated REMOTE.\n"
             << "You have the choice of upgrading Remote permits to allow access "
             << "to any lots designated STUDENT, which costs a little more but "
             << "provides better parking options.\n\n";

        cout << "The Resident Permits are more expensive, because they generally "
             << "allow parking in prime locations closer to students' Residence Halls.\n"
             << "You can purchase this permit for any number of months, and you "
             << "may then park in any lots designated RESIDENT.\n"
             << "You have the choice of upgrading Resident permits to allow access "
             << "to the best Residence Hall parking lots designated RESIDENT RESERVED; "

             << "this costs a one-time upgrade fee as well.\n\n";

        cout << "The Garage Permits are the most expensive.\n"
             << "They allow charge-free parking in one of the two parking garages "
             << "located around campus: Harmon or Garland.\n"
             << "The Garland Garage permits cost slightly more since the facility "
             << "is brand new and located immediately next to the Health Center and Bookstore.\n\n";
    }
}

//---------------------------------------------------------------------------
// Name: GetPurchaseChoice
// Parameters: none
// Returns: char; the selection of permit to be purchased
// Purpose: This function asks the user which permit they would like to buy
//---------------------------------------------------------------------------
char GetPurchaseChoice()
{

    char PurchaseChoice;

    cout << "Which permit would you like to purchase next?\n";
    cout << "Type R for Remote Permit, S for Resident Permit,\n"
         << "G for Garage Permit, or N for No Permit\n";
    cin >> PurchaseChoice;

    //User I/O error-checking
    PurchaseChoice = toupper(PurchaseChoice);
    while(PurchaseChoice != 'R' && PurchaseChoice != 'S'
          && PurchaseChoice != 'G' && PurchaseChoice != 'N')
    {
        cout << PurchaseChoice << " is invalid input.\n";
        cout << "Type R for Remote Permit, S for Resident Permit,\n"
             << "G for Garage Permit, or N for No Permit\n";
        cin >> PurchaseChoice;
        PurchaseChoice = toupper(PurchaseChoice);
    }


    return PurchaseChoice;
}

//---------------------------------------------------------------------------
// Name: GetPurchaseChoice
// Parameters: Months, integer, reference, passes back the number of months requested
// Returns: none
// Purpose: This function asks the user how many months would like to buy
//---------------------------------------------------------------------------
void GetMonths (int &Months)
{
   cout << "How many months would you like to buy a Remote Permit for?\n";
   cin >> Months;
   while(Months < 0) //User I/O error-checking
   {
       cout << "Please choose a non-negative number of months:";
       cin >> Months;

   }
}


//---------------------------------------------------------------------------
//      This is the main program that you need to write
//---------------------------------------------------------------------------
int main ()
{
   // Declarations

   // Print your name and UAID
        cout << "Name" << endl;       
        cout << "ID" << endl;           

   // Loop until the user is done
           
       // Must reset variable values on each loop


       // Print the main menu describing the permits offered
           
       // Ask the user what permit they want to purchase next

       // Call functions to figure out the price of the permit
           //
       // Add the permit price to a running total

       //Prompt to continue or quit

   // When the loop is done
   // Sell the user Game Day passes for football parking

   // Print out the total amount of all the permits and Game Day passes that the student has purchased
   // to 2 decimal places, with a $

   return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Homework with functions

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Homework with functions

Posted 05 October 2010 - 10:14 PM

I would suggest starting here:

// Loop until the user is done


create either a while() loop or a do...while() loop that asks if the user is done continuing the loop if they indicate no. Once you have that working, then you can add each of the required functions in order starting with the menu.

The given code has examples of both kinds of loops.

This post has been edited by n8wxs: 05 October 2010 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1