2 Replies - 4876 Views - Last Post: 04 October 2012 - 06:05 AM Rate Topic: -----

#1 carl edem  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
  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
   int cartotal;
   double cash;
void Tollbooth::payingCar()
    cartotal = 1 + cartotal;
    cash = .50 + cash;
void Tollbooth::nopayCar()
    cartotal = 1 + cartotal;
void Tollbooth::display()
    cout << "Car Total =" << cartotal << endl;
    cout << "Cash Total =" << cash << endl;
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;
    } while(ch != ESC);
    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  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2030
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6237
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1