1,105,371 Community Members

Problem with program involving classes

Member Avatar
jcflore3
Newbie Poster
3 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, you guys helped me on a program about a month ago and it really helped me get through the program. Therefore I really appreciate what you all do and thank you for the help last time

I am having trouble finishing this program on classes the assignment is this:

Write a program to simulate a tollbooth at a bride. Cars passing by the booth are expected to pay a fifty cent toll. Mostly the cars do, but some cars go without paying. The tollbooth keeps a track of the number of cars that have gone by, and the total money collected.
Model this tollbooth with a class tollbooth. 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.

This is what I have so far:

# include <iostream>
# include <iomanip>
using namespace std;
// Class declaration
class Tollbooth
{
private:
int cartotal;
float cash;
public:
Tollbooth (int = 0, float = 0); // constructor
void payingCar (int, float); // Function to increment the car total and cash total by 0.50
void nopayCar(int); //Function to increment the car total, but not cash total.
void display(); //Function to display the two member variables
};
// implementation section
Tollbooth::Tollbooth(int num, float num1)
{
cartotal = num;
cash = num1;
}
void Tollbooth::payingCar(int num, float num1)
{
num++;
num1 = num1 + 0.50;

return;
}
void Tollbooth::nopayCar(int num)
{
num++;
return;
}
void Tollbooth::display()
{
cout << "\nNumber of cars gone by: " << num << endl;
cout << "Total money collected: " << num1 << 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";

do
{
ch=getche();
if(ch=='0')
toll.payingCar();//call appropriate function
if(ch=='1')
toll.nopayCar();// write functionality;
} while(ch != ESC);
toll.display();
return 0;
}


The problem is that I am getting six errors and I am at a lost as to how to fix them.
These are the errors:

Warning 1 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data \
Error 2 error C2065: 'num' : undeclared identifier
Error 3 error C2065: 'num1' : undeclared identifier
Error 4 error C3861: 'getche': identifier not found
Error 5 error C2660: 'Tollbooth::payingCar' : function does not take 0 arguments
Error 6 error C2660: 'Tollbooth::nopayCar' : function does not take 0 arguments
Error 7 error C2065: 'ESC' : undeclared identifier

Anything will help, Thank you.

Member Avatar
Lerner
Nearly a Posting Maven
2,416 posts since Jul 2005
Reputation Points: 579 [?]
Q&As Helped to Solve: 407 [?]
Skill Endorsements: 16 [?]
 
0
 

The variables num and num1 in the display function are not declared. That's what the compiler is complaining about.

Instead of ch = getche() I'd use cin >> ch; since this is C++.

You have declared and defined noPayCar() as taking an int but don't pass it anything in main() and you defined and declared payingCar() as taking an int and a float but you don't pass it anything when calling it in main().

However, the biggest error is not understanding the relationship between num and carTotal and num1 and cash.

BTW::Please use code tags!!

Member Avatar
jcflore3
Newbie Poster
3 posts since Nov 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok thanks man, I think I fixed most of the problems, except now I still get three errors, this is my new program:

# include <iostream>
# include <iomanip>

using namespace std;
// Class declaration
class Tollbooth
{
private:
int cartotal;
float cash;
public:
Tollbooth (int = 0, float = 0); // constructor
void payingCar (int, float); // Function to increment the car total and cash total by 0.50
void nopayCar(int); //Function to increment the car total, but not cash total.
void display(int, float); //Function to display the two member variables
};
// implementation section
Tollbooth::Tollbooth(int num, float num1)
{
cartotal = num;
cash = num1;
}
void Tollbooth::payingCar(int num, float num1)
{
cartotal = num++;
cash = num1 + 0.50;

return;
}
void Tollbooth::nopayCar(int num)
{
cartotal = num++;
return;
}
void Tollbooth::display(int num, float num1)
{
cout << "\nNumber of cars gone by: " << cartotal << endl;
cout << "Total money collected: " << cash << endl;
return;
}
int main()
{
Tollbooth a(int num, float num1), b(int num), toll(int num, float num1);
double ch, ESC;
cout<<"press 0 for non-paying cars / press 1 for paying cars / press Esc to exit program";
cin >> ch;

do
{
ch;
if(ch=='0')
{
a.payingCar();//call appropriate function
}
if(ch=='1')
{
b.nopayCar();// write functionality;
}
} while(ch != ESC);
toll.display();
return 0;
}

Errors are:

Warning 1 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
2 error C2228: left of '.payingCar' must have class/struct/union
Error 3 error C2228: left of '.nopayCar' must have class/struct/union
Error 4 error C2228: left of '.display' must have class/struct/union

How do I call the functions properly.

Oh yeah and what are code tags.

Thankyou

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

Oh yeah and what are code tags.

Put your code inside [code] and [/code]. See here for more information:
http://www.daniweb.com/techtalkforums/announcement8-3.html
They simply make your code easier to read by putting the code in monospace font, and will even color your code for you if you use [code=c] or [code=cplusplus].

Space missing:

using namespace std;

'p' and colon missing:

void Tollbooth::payingCar(int num, float num1)
{

It's rather odd here, because all of these functions you declared with at least one parameter, and here you're calling them without any:

do
{
ch;
if(ch=='0')
{
a.payingCar();//call appropriate function
}
if(ch=='1')
{
b.nopayCar();// write functionality;
}
} while(ch != ESC);
toll.display();

You previously declared these functions as (int num, float num1), (int num), and (int num, float num1), respectively.

Just to make it clear, you probably want to use cin to get some more data about the cars before you call payingCar(), nopayCar, etc.. (sorry I can't really think right now)

Member Avatar
may4life
Junior Poster in Training
57 posts since Oct 2006
Reputation Points: 2 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

You've got to understand you only need to be dealing with one instance of class Tollbooth. You declare 3 in your main! a,b,and toll.
Just a hint, you dont need any parameters in your accessor functions! You can simply manipulate the variables cartotal and cash without any nums or num1s..
for example, your payingcar function could be written

void Tollbooth::payingCar()
{
    cartotal++;
    cash += 0.50;
}

and in your main you would call this function by saying:

booth.payingCar();

and this will increment the cartotal variable, and also increment the cash variable.
A similar method can be used for non paying cars and the display function will only have to output these two variables.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article