You are here:

C++/Parking Garage computations

Advertisement


Question
Hello, my COP1000 homework assignment states:
: A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and prints the parking charges for each of 10 customers who parked their cars in this garage yesterday. Program requirements:
a.   The program should call the function enterHoursParked to capture the hours parked for each customer from the keyboard.
b.   Use an array to save the data on each car parked.
c.   The program should call the function calculateCharges to determine the charge for each customer.
d.   The program should calculate the total of yesterday's receipts.


e.   The program should call the function printCharges to output the charges and totals in a neat tabular format as demonstrated here:
Car   Hours   Charge
1   1.5   2.00
2   4.0   2.50   
3   24.0   10.00
etc.
Total   29.5   14.50

2.   Additional solution requirements:
a.   Remember to start your program with descriptive comments identifying the program name, author, date, purpose, and any assumptions made.
b.   Make your variable and constant declarations at the beginning of the code block (with the exception of the any loop control variables you use…your choice).
c.   You must pass arguments from main to the functions; no access to global variables inside the functions allowed!

My code so far is as follows(sorry for  being so messy!)

#include <iostream>
#include <iomanip>

using namespace std;

const int CARS = 10;
  int hours[CARS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  hours[0] = 1;

int enterHoursParked(int i, int hoursParked)
{
  
  cout << "Enter the hours parked for car" << (i + 1)<<", example 5.5: ";
  cin >> hours[CARS];

  return hoursParked;
}

int calculateCharges(int hoursParked, int)
{
  int finalCharge;
  
  if (hoursParked <= 3)
     finalCharge = 2;
  else if (hoursParked >= 19)
     finalCharge = 10;
  else if (hoursParked > 3)
     finalCharge = 2 + (hoursParked - 3) * .50;
  
  return finalCharge;
}
int printCharges(int hoursParked, int finalCharge)
{
  cout << "Car       Hours       Charge";

  for (int i = 0; i <= 10; i++)

     cout << i << hoursParked << finalCharge;
  
  return 0;
}


int main()
{
  cout << "Welcome!" << endl;
  int hours[CARS];

  for (int i = 0; i <= 10; i = i + 1)
  {
     enterHoursParked( hours[CARS], i);
  
     int calculateCharges(int hoursParked);
  }
  

  

  int printCharges(int hours[], int finalCharge);
  
   

return 0;
}

sorry if this is lengthy.. thanks for the help in advance!


Answer
#include <stdio.h>
#include<iostream>

#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

const int CARS = 10;
static int hours[CARS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void enterHoursParked()
{
for(int i=0;i<CARS;i++)
{
       cout << "Enter the hours parked for each car"<< i;
       cin >> hours[i];
}

}

float calculateCharges(int ilen)
{
float finalCharge;

if (ilen <= 3)
finalCharge = 2;
if (ilen >= 19)
finalCharge = 10;
if (ilen > 3)
finalCharge = 2 + (float)(ilen - 3) * .50;


return finalCharge;
}
void printCharges(int carno, int hours, float finalCharge)
{
if(carno == 0)    
cout << "Car     Hours    Charge\n";
else
cout << (carno)<<"     "<< hours<<"      "<< finalCharge<<"\n";


}


int main()
{

cout << "Welcome!" << endl;
enterHoursParked();

for (int i = 0; i <= CARS; i++)
{
float finalCharge = calculateCharges(hours[i]);
printCharges(i,hours[i],finalCharge );

}

 
int l;
cin>>l;  

return 0;
}

Let me know if it does not work

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


rakshitha

Expertise

Difficult questions related to C .

Experience

C ,vc ,opc ,windows CE, ActiveX

Education/Credentials
Masters in electrical engineering

©2016 About.com. All rights reserved.