473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

HELP! C++ program for simulating tollbooth

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
5 7068
Banfa
9,065 Expert Mod 8TB
Bad choice of exit key, looks like it may be used in interpreting cosole input.

Use q (for quit) instead.
Nov 30 '06 #2
Keo932
8
i wish i could but the instructions call for the esc key to be pressed
Nov 30 '06 #3
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
nachu
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
9,065 Expert Mod 8TB
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

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

Similar topics

9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.