# Tollbooth Simulation

Page 1 of 1

## 2 Replies - 4888 Views - Last Post: 04 October 2012 - 06:05 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294229&amp;s=738857efcfe33edea69dce826549cfa7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 carl edem

Reputation: 0
• Posts: 3
• Joined: 04-October 12

# Tollbooth Simulation

Posted 04 October 2012 - 04:43 AM

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 pesewas toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected.

Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0.

A member function called payingCar() increments the car total and adds 0.50 to the cash total.

Another function, called nopayCar(), increments the car total but adds nothing to the cash total.

Finally, a member function called display() displays the two totals. Make appropriate member functions const.

Include a program to test this class. This program should allow the user to push one key to count a paying car, and another to count a nonpaying car. Pushing the Esc key should cause the program to print out the total cars and total cash and then exit.

```#include <iostream>

using namespace std;
int getche();
class Tollbooth
{
public:
Tollbooth (int = 0, double = 0);  // constructor
void payingCar ();  // Function to increment the car total and cash total by 0.50
void nopayCar(); //Function to increment the car total, but not cash total.
void display(); //Function to display the two member variables

private:
int cartotal;
double cash;
};
void Tollbooth::payingCar()
{
cartotal = 1 + cartotal;
cash = .50 + cash;
return;
}
void Tollbooth::nopayCar()
{
cartotal = 1 + cartotal;
return;
}
void Tollbooth::display()
{
cout << "Car Total =" << cartotal << endl;
cout << "Cash Total =" << cash << endl;
return;
}
int main()
{
Tollbooth toll;
char ch;
cout<< "press 0 for non-paying cars  press 1 for paying cars   press Esc to exit program"; // ASCI for ESC is 27;
do
{
ch=getche();
if(ch=='0')
toll.payingCar();
if(ch=='1')
toll.nopayCar();
} while(ch != ESC);
toll.display();
return 0;
}

int getche()
{
int num;
cin >> num;
return num;
}
```

MOD EDIT: Added text from "Gimme The Codez" original post.

This post has been edited by JackOfAllTrades: 04 October 2012 - 06:06 AM
Reason for edit:: added [code][/code] tags - learn to use them yourself

Is This A Good Question/Topic? 0

## Replies To: Tollbooth Simulation

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2030
• Posts: 3,997
• Joined: 30-May 10

## Re: Tollbooth Simulation

Posted 04 October 2012 - 04:55 AM

As well as not using code tags, you're also
- 3 years late
- giving too much away without effort from the original poster.

Remember, we're here to help people work through the problems themselves, not show up "all bright eyed and bushy tailed" to impress others with our coding skills, and hand over an answer on a plate.

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: Tollbooth Simulation

Posted 04 October 2012 - 06:05 AM

Split from old topic.

OK, now the NEXT thing you have to do is tell us what your problems are.