I am finishing up my program to simulate a tollbooth by using classes. What happens is cars passing by the booth are expected to pay a fifty cent toll. The program keeps track of the number of cars that have gone by (paid and unpaid), and the total money collected.
The two data items are of type int to hold the total number of cars and type float to hold the total amount of money collected.
A constructor initializes both these items to 0.
A member function called payingCar() increments the car total and adds 0.50 to the cash total.
Second member function called nopayCar() increments the car total but adds nothing to the cash total.
Finally, a member function display() displays the two total.
Im having trouble finding out how to use the ESC key to exit the do while loop, as u will see below. I read somewhere that the ASCI for ESC is 27 but have no idea how to input it into the program. Im currently stuck where i am but will continue to try and work out the member function errors. If anyone could offer some help it would be greatly appreciated. Thanks!
-Keo
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int getche();
- class Tollbooth
- {
- public:
- Tollbooth (int = 0, float = 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;
- float 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;
- }