Help with Formatting

K

Keith

Ok, so here is the code I have so far........my question is whether it is
possible to hold onto the amount of hours for each car entered without
erasing it through the next entry. I need a table format like this to appear
at the end with a total for all three cars. How do I handle this?
Table:
Car Hrs Rate
1 3 2.00
2 4.0 2.50
3 24 10.00
Totals: 31 14.50

Code:
// Program computes charges for parked cars and returns daily total

#include <iostream>

#include <conio.h>

#include <cmath>

using std::cout;

using std::cin;



double calculateCharges ( double ); // function prototype

// Explanation of function calculateCharges

double calculateCharges(double hrs)

{

//double hrs;

double charge;

double extracharge;

double result;

charge = 2.00; // first 3 hours of parking cost $2.00

extracharge = charge + (hrs * .50); // any hour (or part therof) after 3
costs .50

result = charge;

if(hrs > 3.0) result = extracharge;

if(hrs > 24.0) result = 10.00;

return result;


}

//function main begins program execution

int main;

int car1;

int car2;

int car3;

double hrs;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car1;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car2;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car3;


Thanks!
 
F

forayer

Keith said:
Ok, so here is the code I have so far........my question is whether it is
possible to hold onto the amount of hours for each car entered without
erasing it through the next entry. I need a table format like this to appear
at the end with a total for all three cars. How do I handle this?
Use a class!

class CarPark {
private:
int car;
double hrs;
public:
void set_car_id(int id) { car = id; }
void set_hours_parked(double hours) { hrs = hours; }
int get_car_id() { return car; }
double get_hours_parked() { return hrs; }
double calc_charges(); // as below
};
double calculateCharges(double hrs)

{

//double hrs;

double charge;

double extracharge;

double result;

charge = 2.00; // first 3 hours of parking cost $2.00

extracharge = charge + (hrs * .50); // any hour (or part therof) after 3
costs .50

result = charge;

if(hrs > 3.0) result = extracharge;

if(hrs > 24.0) result = 10.00;

return result;


}
No offence, but always try to keep it simple.

if(hrs > 24) return 10.00;
else if(hrs > 3) return (2.00 + 0.50*(hrs-3));
else return 2.00;
//function main begins program execution

int main;

int car1;

int car2;

int car3;

double hrs;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car1;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car2;

cout << "Enter the amount of hours parked :";

cin >> hrs >> car3;


Thanks!
int main()
{
CarPark cars[2];
int car_id;
double hrs;

cin >> hrs >> car_id;
cars[0].set_car_id(car_id);
cars[0].set_hours_parked(hrs);
cin >> hrs >> car_id;
cars[1].set_car_id(car_id);
cars[1].set_hours_parked(hrs);

for(int i=0; i<2; i++)
cout << "Car #" << car.get_car_id()
<< " was parked for " << cars.get_hours_parked()
<< " hours. Amount due = " << cars.calc_charges()
<< endl;
return 0;
}
 
F

forayer

for(int i=0; i<2; i++)
cout << "Car #" << car.get_car_id()
<< " was parked for " << cars.get_hours_parked()
<< " hours. Amount due = " << cars.calc_charges()
<< endl;


Oops, my mistake. For a table listing try this:

//heading
cout << setw(8) << "Car" << "\t"
<< setw(8) << "Hours" << "\t"
<< setw(8) << "Rate" << endl;
cout.precision(2);
// listing
cout << setw(8) << car.get_car_id() << "\t"
<< setw(8) << car.get_hours_parked() << "\t"
<< setw(8) << car.calc_charges() << endl;

rgds,
forayer
 
W

wittempj

In addition to the class usage idea of previous poster I would add
three more things:
- give the class an overload operator <<
- use a vector to store the cars
- print the header via a manipulator

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

class CarPark {
private:
int car;
double hrs;
public:
void set_car_id(int id) { car = id; }
void set_hours_parked(double hours) { hrs = hours; }
int get_car_id() { return car; }
double get_hours_parked() { return hrs; }
double calc_charges(); // as below
friend ostream& operator << (ostream& os, CarPark& cp);
};

ostream& header(ostream& os)
{
os << setw(8) << "Car" << "\t"
<< setw(8) << "Hours" << "\t"
<< setw(8) << "Rate";
}

ostream& operator << (ostream& os, CarPark& cp)
{
os.precision(2);
os << setw(8) << cp.get_car_id() << "\t"
<< setw(8) << cp.get_hours_parked() << "\t"
<< setw(8) << cps.calc_charges();
return os;
}

double CarPark::calc_charges()
{
if(hrs > 24)
{
return 10.00;
}
else if(hrs > 3)
{
return (2.00 + 0.50*(hrs-3));
}
else
{
return 2.00;
}
}

int main()
{
vector<CarPark> cars;
vector<CarPark>::iterator it;

CarPark cp;
int car_id;
double hrs;

for (int i = 0; i < 2 ; ++i)
{
cin >> hrs >> car_id;
cp.set_car_id(car_id);
cp.set_hours_parked(hrs);
cars.push_back(cp);
}

cout << header << endl;
for(it = cars.begin(); it != cars.end(); ++it)
{
cout << (*it) << endl;
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

help with arrays 3
Wants help in c program made with arrays and function 0
CIN Input #2 gets skipped, I don't understand why. 1
I need help 1
Calculating overtime 2
Chatbot 0
Formatting 4
Formatting 1

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top