By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

HELP! C++ program for simulating tollbooth

P: 8
Hello all,

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
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int getche();
  7.  
  8.  
  9.  
  10.  
  11. class Tollbooth
  12. {
  13. public:
  14.  
  15.   Tollbooth (int = 0, float = 0);  // constructor
  16.  
  17.   void payingCar ();  // Function to increment the car total and cash total by 0.50
  18.  
  19.   void nopayCar(); //Function to increment the car total, but not cash total.
  20.  
  21.   void display(); //Function to display the two member variables
  22.  
  23. private:
  24.  
  25.    int cartotal;
  26.    float cash;
  27. };
  28.  
  29. void Tollbooth::payingCar()
  30. {
  31.     cartotal = 1 + cartotal;
  32.     cash = .50 + cash;
  33.  
  34.     return;
  35. }
  36.  
  37. void Tollbooth::nopayCar()
  38. {
  39.     cartotal = 1 + cartotal;
  40.  
  41.     return;
  42. }
  43.  
  44. void Tollbooth::display()
  45. {
  46.     cout << "Car Total =" << cartotal << endl;
  47.     cout << "Cash Total =" << cash << endl;
  48.  
  49.     return;
  50. }
  51.  
  52.  
  53.  
  54.  
  55. int main ( )
  56. {
  57.     Tollbooth toll;
  58.  
  59.  
  60.     char ch;
  61.     cout<< "press 0 for non-paying cars / 
  62.                     press 1 for paying cars / 
  63.                     press Esc to exit program"; // ASCI for ESC is 27;
  64.     do
  65.     {
  66.         ch=getche();
  67.         if(ch=='0')
  68.             toll.payingCar();
  69.         if(ch=='1')
  70.             toll.nopayCar();
  71.     } while(ch != ESC);
  72.  
  73.     toll.display();
  74.  
  75.     return 0;
  76. }
  77.  
  78. int getche()
  79. {
  80.     int num;
  81.     cin >> num;
  82.     return num;
  83. }
  84.  
Nov 30 '06 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Bad choice of exit key, looks like it may be used in interpreting cosole input.

Use q (for quit) instead.
Nov 30 '06 #2

P: 8
i wish i could but the instructions call for the esc key to be pressed
Nov 30 '06 #3

P: 1
you can use escape- it just eats more memory to do it
theres an API called GetAsyncKeyState(byte VirtualKey) <-check out msdn on it

anyways, put a call to that in seperate thread that you'll start before the while loop. the thread will look something like
thread name( and params)
{
while(1)
if(GetAsyncKeyState(VK_ESCAPE)) exit(0);
}


threads are easy to learn and use, they just slow a program up big time.
basically threads are like this:

you have the parent program-the tollbooth program
1 main block of code to exexucte (int main)- the while loop
and 1 thread- the thread that scans for the escape key hit

int main and the thread will execute simultaneously
Dec 29 '06 #4

P: 15
THE CONCEPT: .(GENERIC)
before exiting the loop or going around the loop getting an option
as "press esc for exit or continue with some other key pressed"
1.in the loop i.e,, while check if it is ==27 .if so quit using a break statement.
2.if not continue.

it look like,IN U R PROGRAM

do
{
CH=GETS();
,........
}while(ch!=27)

GIVE A TRY .I M NOT SURE IF IT WILL WORK .I WILL TRY MYSELF AND REPLY IF I GET WITH IT.....
Jan 2 '07 #5

Banfa
Expert Mod 5K+
P: 8,916
I think the problem is that you have implemented your own getch using cin. cin definately does some processing of the input before passing it onto the program as does getchar and getc from stdio.h, for instance these functions require an enter key press after the required key has been pressed.

However this small program I have written detects an escape key press

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #include <conio.h>
  4.  
  5. int main ()
  6. {
  7.     int in;
  8.  
  9.     do
  10.     {
  11.         in = _getch();
  12.  
  13.         if (in == 27)
  14.             printf("\t%d '%c'  -  ESCAPE ESCAPE ESCAPE\n", in, in);
  15.         else
  16.             printf("\t%d '%c'\n", in, in);
  17.     }
  18.     while(in != 'q' && in != 'Q');
  19.  
  20.     return 0;
  21. }
However note I have used _getch() and included conio.h These are not standard functions but part of the platform defined functions for direct access to the keyboard (in MSVC++). What every platform you are using it is likely that it will have direct access functions like these too but it will make your program platform dependent.

However I think that is the only way to detect the escape key.
Jan 4 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.