tollbooth program

This is a discussion on tollbooth program within the C++ Programming forums, part of the General Programming Boards category; actually the problem is that i have to make a program for tollbooth the format is cars passing the booth ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    Arrow tollbooth program

    actually the problem is that i have to make a program for tollbooth
    the format is cars passing the booth are expected to pay 50 cent toll. mostly they do but sometimes a car goes without paying. the tollbooth keeps track of the number of cars that have gone by, and of the total money collected.
    i hav to use two data members one for total number of cars and the other for the total cash.
    a member function payingcar() increments the car total and adds 0.50 to the cash total. anothjer member function called nopaycar() increments the car total but adds nothing to total cash.
    i have to use one key to count a paying car and another key to count a non paying car. pushing Esc should cause the program to print out the total cars and total cash.
    kindly guide me in these fields
    1. give a hint of material of member functions
    2. how to use keys for paying and non paying cars bcz i have used only numbers yet
    3. what for Esc button bcz it does not appears in the editor

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    All the information you need is there and is easy to follow.

    Show what you have attempted so far and what is not working and I am sure somebody will help.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Code:
    # include <iostream>
    # include <conio.h>
    using namespace std;
    class tollbooth
    {
    private:
    	long int total_cars;
    	float total_cash;
    public:
    	void payingcar()//inline function
    	{
    		total_cars++;
    		total_cash+=0.50;
    	
    	}//end of function
    	void nonpayingcar()//inline function
    	{
    		total_cars++;
    	}//end function
    
    	void print () const//inline function
    	{
    		cout<<"total cars passed:"<<total_cars<<endl;
    		cout<<"total cash received:"<<total_cash<<endl;
    	
    	}//end function
    
    
    };//end class
    int main ()
    {
    
    	tollbooth lahore;
    	char push_key,a,b;
    	if (push_key==a)
    	{
    		lahore.payingcar();
    	
    	}//end if
    	else if (push_key==b)
    	{
    		lahore.nonpayingcar();
    	}//end else
    	
    	getch ();
    	
    	
    
    }//end main

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    now the problem is which loop should i use and when i press "a" my compilor gives a statement and gives two options "continue" "break"

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    You are not obtaining any input? Are you familiar with 'cin'? I would recommend using that.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Code:
    # include <iostream>
    # include <conio.h>
    using namespace std;
    class tollbooth
    {
    private:
    	long int total_cars;
    	float total_cash;
    public:
    	void payingcar()//inline function
    	{
    		total_cars++;
    		total_cash+=0.50;
    	
    	}//end of function
    	void nonpayingcar()//inline function
    	{
    		total_cars++;
    	}//end function
    
    	void print () //inline function
    	{
    		cout<<"total cars passed:"<<total_cars<<endl;
    		cout<<"total cash received:"<<total_cash<<endl;
    	
    	}//end function
    
    
    };//end class
    int main ()
    {
    	tollbooth lahore;
    	char push_key;
    	cin>>push_key;
    	do
    	{
    	if (push_key=='1')
    	{
    		lahore.payingcar();
    	
    	}//end if
    	if (push_key=='0')
    	{
    		lahore.nonpayingcar();
    	}//end else
    		cin>>push_key;
    	}//end do
    	
    	while (push_key!='3');
    	
    		lahore.print ();
    	
    
    	getch ();
    	
    	
    
    }//end main

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    now i have corrected my program but when i push "3" it displays address not the number of cars and cash kindly guide me.
    actually darren i was focusing on class object and member functions thats why i forgotten and that was really funny.
    kindly help me

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    It is because you are not initialising your two variables when you create the tollbooth object.

    Have you learnt about constructors yet? My suggestion is to create a default constructor that sets both your variables to zero when you create a new tollbooth instance.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i have written constructor and it is now working.
    i know but according to my knowledge compiler calls a default constructor and intializes the data members itself.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by rafay_07 View Post
    i have written constructor and it is now working.
    i know but according to my knowledge compiler calls a default constructor and intializes the data members itself.
    That's incorrect. If, when you create an instance of a class, you need to initialise data members, then you need to declare a constructor. Otherwise the information stored in the memory locations of those data members will simply be garbage and is what you were seeing as output before you wrote a constructor. If your class has no data members then an implicit compiler generated constructor would generally be fine.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    darren many many thanks, u really solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21