A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $.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. 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 3 customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate and print the total of yesterday’s receipts.

The program should use the function calculateCharges to determine the charge for each customer. Your output should appear in the same format as shown above.


Pass one variable to calculateCharges. Do not have multiple variables for the customer’s cars, e.g., car1, car2, car3, or hours1, hours2, hours3 etc. Use a for loop to handle the repetition.]


I’m having a heck of a time developing the for loop to handle the repetition. Can someone provide me with an example that I can try? I can capture one, but not all three. Thanks

Okay. Post the code that calculates the fee for one customer. Then we will help you on the repetition part.

double calculateCharge ( double x )
{
    double charge;



    if (x <= 3)

                charge = 2;

                else if (x >19)

                        charge = 10;

                else if (x > 3)

                    charge = 2 + (x - 3) * (.5);

    return charge;

}

Okay that is the function calculateCharge but that was not what I asked for.I want you to write a program that gets the number of hours parked for one customer as user input and print out the fee calculated by using calculateCharge. After that it is a simple modification to convert it for 3 customers.

Sorry if this is ugly, I haven't had time to clean it up yet, I just got it working. Please note, I'm new at this, so be gentle. :)

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fixed;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>


//function prototype
double calculateCharge(double);

int count;

int main()
{       
    int num;

//    for ( int hours1 = 1; hours1 <= 3; hours1++ )
    
    cout << "enter hours parked  ";
    cin >> num;
     
    
    cout<< setw(5)<< "Car 1" << setw(10)<< "Hours" << setw(3)<< num << setw(15)<< "Charge\n";     
    //cout<< setw(12)<< num;
 

    //set floating point number format
    cout << fixed << setprecision( 2 );
    
    cout << setw(32) << calculateCharge (num) << endl;

    count = 1;

        while ( count <=3 ) {        

                if (count == 3)

                        num = 1;

                else if (count == 2)

                        num = 2;

                else if (count == 1)

                        num = 3;
        }


        

    return 0;




}//end main 

double calculateCharge( double x)
{

double charge;

            

    if (x <= 3)

                charge = 2;

                else if (x >19)

                        charge = 10;

                else if (x > 3)

                    charge = 2 + (x - 3) * (.5);

    return charge;
}

Hmmm how about something like:

int hours = 0 ;
for( int i = 0; i < 3; ++i )
{
   cout << "Enter the hours parked: " ;
   cin >> hours ;
   // call your function and display the output in the format you want.

  // do whatever processing you want to do.

}
// end the for loop

Good effort. There were some parts where you could clear things up a little. So I will keep my end of the promise and here is a working program. You may also want to read up a little more about [search]for, while and do while in C and C++[/search]

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fixed;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>


//function prototype
double calculateCharge(double);


int main()
{
    int num;
    int count = 1;
    while ( count <= 3 )
    {
        cout << "enter hours parked ";
        cin >> num;
        cout<< setw(5)<< "Customer Name" << setw(10)<< "Hours" << setw(15)<< "Charge\n";
        cout<< setw(5)<< "Car " << count << setw(14)<< num << setw(16)<< fixed << setprecision( 2 ) << calculateCharge (num) << endl;
        count = count + 1;
    }
    return 0;
}//end main

double calculateCharge( double x)
{
    double charge;

    if (x <= 3)
        charge = 2;
    else if (x >19)
        charge = 10;
    else if (x > 3)
        charge = 2 + (x - 3) * (.5);
    return charge;
}

Thanks for your input. I'll work on it tonight, I can't wait! Thanks !!!

The code workes great, thank you! I do have one more question. I need to calculate the total hours and charges. Would the best way to do this is to have another formula at the bottom like CalculateCharges or do a cout with the num+num+num?

TOTAL           23hrs    $10.00

Here's my code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fixed;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>


//function prototype
double calculateCharge(double);
double Totalhours(double);

int count;

int main()

{
    int num = 0;

    int count = 1;   

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

    while ( count <= 3 )
    {
        cout << "enter hours parked ";
        cin >> num;
        cout<< setw(10)<< "Hours" << setw(15)<< "Charge\n";
        cout<< setw(5)<< "Car " << count << setw(14)<< num << setw(16)<< fixed << setprecision( 2 ) << calculateCharge (num) << endl;

        count = count + 1;
    }

        return 0;
}//end main



double calculateCharge( double x)
{

double charge;       

    if (x <= 3)
          charge = 2;

    else if (x >19)
           charge = 10;

    else if (x > 3)
            charge = 2 + (x - 3) * (.5);

return charge;

}

I need to calculate the total hours and charges. Would the best way to do this is to have another formula at the bottom like CalculateCharges or do a cout with the num+num+num?

Create a variable called total_hours just after count. Inside the loop add num to it. After exiting the while loop cout the value of total_hours. Same for the charges.

Does my for loop look correct? Also, would I do the same with the total_hours as you describe above if I'm not using the While? I'm sorry, these must be dumb questions, but I'm really new at this and just trying to understand how this is working. thanks

#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::fixed;

#include <iomanip>

using std::setw;
using std::setiosflags;
using std::setprecision;

#include <cmath>


//function prototype
double calculateCharge(double);
double Totalhours(double);

int count;
int total_hours;
int main()

{
    int num;
    
    int count = 1;   


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

//    while ( count <= 3 )
    {
        cout << "enter hours parked ";
        cin >> num ;
        cout<< setw(10)<< "Hours" << setw(15)<< "Charge\n";
        cout<< setw(5)<< "Car " << count << setw(14)<< num << setw(16)<< fixed << setprecision( 2 ) << calculateCharge (num) << endl;
                
        count = count + 1;


    }
        

        return 0;
}//end main

THe sum of all hours or the total hours would be the sum of the individual hours which you accept from the user in th for loop. SO the logical thing would be to add to the total hours, the value of hours entered by the user.
Something like:

int total_hours = 0 ;
int total_charge = 0 ;

for( int i = 0; i < 3; ++i )
{
    // accept input from the user as "num"
    // calculate the charge for the hours inputted by user as "charge"

   total_hours = total_hours + num ;
   total_charge = total_charge + charge ;
}

Has anyone ever referred to you as “MASTER”??? This has helped me out immensely. Thank you for helping us newbie’s out.

Sir.......! how can we pass a value of an array without using index of an array i.e can you anyone help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.