473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with Formatting

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!
Jul 23 '05 #1
3 1280
Keith wrote:
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[i].get_car_id()
<< " was parked for " << cars[i].get_hours_parked()
<< " hours. Amount due = " << cars[i].calc_charges()
<< endl;
return 0;
}
Jul 23 '05 #2
forayer wrote:
Keith wrote:
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?

for(int i=0; i<2; i++)
cout << "Car #" << car[i].get_car_id()
<< " was parked for " << cars[i].get_hours_parked()
<< " hours. Amount due = " << cars[i].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[i].get_car_id() << "\t"
<< setw(8) << car[i].get_hours_parked() << "\t"
<< setw(8) << car[i].calc_charges() << endl;

rgds,
forayer
Jul 23 '05 #3
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;
}

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dinoo | last post by:
I need your help I want to know what other options user have if he does not have Microsoft Word on his machine I would like to use word sort of application for formatting documents, these...
4
by: Wayne Aprato | last post by:
I have a simple database which was originally written in Access 97. When converted to Access 2000 file format it ran flawlessly in Access 2002. I've just tried to run it in Access 2003 and I am...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
3
by: trint | last post by:
Ok, I have a textbox that I want to check as one types each charactor for Currency formatting, another one for Date formating. Any help is appreciated. Thanks, Trint
4
by: hope | last post by:
Hi, How can I format a string field using Data Formatting Expression property in datagrid? For example: format last name from BROWN to Brown. Thanks
2
by: jodyblau | last post by:
I'm not certain that what I am trying to do is possible; in any event I haven't been able to figure it out. Here is what I am trying to do: I have one table that has a list of cases I'm working...
2
by: sgtted71 | last post by:
I have searched the Word Help and the Access Help, I have consulted the well used Access Bible that sits behind my desk, and to no avail...so now I consult the mighty brain trust that is...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
4
by: n.phelge | last post by:
I need to perform an XSLT to set the namespace on some XML and I need to preserve the original document line formatting to assist with error handling (so the line number from any schema validation...
8
by: babyangel43 | last post by:
Hello, I have a query set up in Access. I run it monthly, changing "date of test". I would like this query to be merged with a Word document so that the cover letter is created in Word, the fields...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.