HELP! C++ program for simulating tollbooth

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Keo932
    New Member
    • Oct 2006
    • 8

    HELP! C++ program for simulating tollbooth

    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

    Code:
     
    #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;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Bad choice of exit key, looks like it may be used in interpreting cosole input.

    Use q (for quit) instead.

    Comment

    • Keo932
      New Member
      • Oct 2006
      • 8

      #3
      i wish i could but the instructions call for the esc key to be pressed

      Comment

      • sharpshooter0327
        New Member
        • Dec 2006
        • 1

        #4
        you can use escape- it just eats more memory to do it
        theres an API called GetAsyncKeyStat e(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(GetAsyncKeyS tate(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

        Comment

        • nachu
          New Member
          • Dec 2006
          • 15

          #5
          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.....

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            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

            Code:
            #include <stdio.h>
            
            #include <conio.h>
            
            int main ()
            {
                int in;
            
                do
                {
                    in = _getch();
            
                    if (in == 27)
                        printf("\t%d '%c'  -  ESCAPE ESCAPE ESCAPE\n", in, in);
                    else
                        printf("\t%d '%c'\n", in, in);
                }
                while(in != 'q' && in != 'Q');
            
                return 0;
            }
            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.

            Comment

            Working...