error

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>

using namespace std;

class tollbooth {
	public:
		tollbooth (int, double); // inits cars and money vars to 0
		void payincar (string answer); //increments the car total and adds .50 to the cash total
		void nopaycar (string answer); //increments the car total but adds no money
		int display (string answer);

	private:
		int cars; //total cars
		double money; //total money made 
};
tollbooth :: tollbooth (int a, double b) {
	money = b;
	cars = a;
}
void tollbooth :: payincar (string answer) {
	if (answer == "p") {
		++cars;
		money = money + .50;
	}
}
void tollbooth :: nopaycar (string answer) {
	if (answer == "n") {
		++cars;
	}
}
int tollbooth :: display (string answer){
	do {
	cout << "P - paid N - Not paid Q - Quit -> ";
	cin >> answer;
	cout << answer << endl;
	}
	while (answer != "q");	
	cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
	if (answer == "q") {
	    return 0;
	}
}
int main () {
	tollbooth toll (0, 0);
	string answer;//this is the answer the user inputs so either p, n, or q	
	toll.display (answer);
	toll.tollbooth (a, b);
	toll.payincar (answer);
	toll.nopaycar (answer);
}

 In function 'int main()':
48:7: error: invalid use of 'tollbooth::tollbooth'
48:18: error: 'a' was not declared in this scope
48:21: error: 'b' was not declared in this scope
 In member function 'int tollbooth::display(std::string)':
43:1: warning: control reaches end of non-void function [-Wreturn-type]

this is what i have now, now i can quit the program with q but thats only when the program has line 48 commented and what i mean by commented is doing this // so basically take that line out but the program the program works as should except it doesnt add the money or cars up
line 48 - you wouldn't call a constructor on an already constructed object, and main doesn't know what a and b are.

The display function just outputs whatever the user inputs. That's why I posted earlier you might end up revisiting the logic in the member functions. Based on the answer the user inputs on line 34, you might call payincar or nopaycar from the display function. Bring the if answer == logic from those two functions into display and call nopaycar if n, payincar if p. e.g.

1
2
3
4
if (answer == "n")
    nopaycar()
else if (answer == "p")
    payincar()


You don't really need lines 40-41. Once the user enters q, the while loop will end. Do you need to return anything from the display function?
Last edited on
to quit the program which q does dont i need to return 0
i am really starting to feel dumb but this class stuff gets me so confused but yea still not working dude i feel im making it way harder than it really is because now the program compiles but still doesnt do as intended
to quit the program which q does dont i need to return 0


If they enter 'q', the while loop will terminate, the display function will terminate, and control will return to the main function. The return 0 can go at the end of the main function.

What do you intend the program to do? What I'm getting from the code you've posted (and I could be wrong) is that the display function controls the operation of the tollbooth. Display is sort of the toll booth collector - it checks each car that goes through the toll and either gets a payment or doesn't (user enters n or p). If the car paid a toll, the collector calls the payincar function, if the car went by without paying, the collector calls the nopaycar function. When the tollbooth closes and the collector goes home (user enters q), the code recaps the number of cars and tolls collected for the day.
exactly so now i have taken the if statement out
1
2
3
if (answer == "q") {
	    return 0;
	}
and also changed display to a void and put return 0; at the end of main and the program runs but doesnt add shit up im going to look at it and see whats going on even though thats what ive been doing and to me it seems as everything should work
Last edited on
Something like this for these functions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void tollbooth :: payincar () {
        ++cars;
	money = money + .50;
}
void tollbooth :: nopaycar () {
	++cars;
}

void tollbooth :: display (){ // I might rename this function to reflect what it does more. operate?
	string answer; //you only seem to need answer here, not other functions
        do {
	cout << "P - paid N - Not paid Q - Quit -> ";
	cin >> answer; 
        if (answer == "n")  // if car didn't pay toll
            nopaycar()  // call that function
        else if (answer == "p")  // if car did pay toll
            payincar() // call the other function
	}while (answer != "q");  // if q entered, exit while loop
	// after loop exited, output totals (maybe the printing/display could be a separate function)
        cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
}

int main () {
	tollbooth toll (0, 0); // construct a tollbooth
	toll.display(); // start running the tollbooth

        return 0;
}
thanks but im still getting a lot of errors for that like string answer it says that it overshadows a parameter
I pulled string answer out of the arguments to the functions.
Hi @mikeal200. I made the code the way that you would like to work.
First of all, I got rid of payincar and nopaycar definitions and carried them inside display. Since you get the answers there. No need to call something extra.

In main(), saying tollbooth toll (0, 0) creates a toll object with the initial values of:
cars = 0, money = 0

Now, since you defined this function as void tollbooth::display (string answer), you have to give an initial value to it when using it inside of main. Doesn't matter what the value is. It will be replaced at cin >> answer.

If you want to remove it check the 2nd code box. There's one as tollbooth::display (), you will see the difference easily.

You also don't need this line of code:
string answer;//this is the answer the user inputs so either p, n, or q

You have already defined it. I guess that was what you wanted. Inputting p p n p q gives the output:
1
2
3
4
5
6
7
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> n
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> q
Total number of cars 4
Total amount collected $1.5


Hope I could help. If you have any questions, feel free to ask.

CodeBox 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

class tollbooth {
	public:
		tollbooth (int, double); // inits cars and money vars to 0
		void display (string answer);

	private:
		int cars; //total cars
		double money; //total money made 
};

tollbooth::tollbooth (int a, double b) {
	money = b;
	cars = a;
}

void tollbooth::display (string answer){
	do {
	cout << "P - paid N - Not paid Q - Quit -> ";
	cin >> answer;

		if (answer == "n") {
			++cars;
		}

		if (answer == "p") {
			++cars;
			money = money + .50;
		}
	}
	while (answer != "q");	
	cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
}
int main () {
	tollbooth toll (0, 0);
	toll.display("NA");
}


CodeBox 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class tollbooth {
	public:
		tollbooth (int, double); // inits cars and money vars to 0
		void display ();

	private:
		int cars; //total cars
		double money; //total money made 
};

tollbooth::tollbooth (int a, double b) {
	money = b;
	cars = a;
}

void tollbooth::display (){
	string answer;
	do {
	cout << "P - paid N - Not paid Q - Quit -> ";
	cin >> answer;

		if (answer == "n") {
			++cars;
		}

		if (answer == "p") {
			++cars;
			money = money + .50;
		}
	}
	while (answer != "q");	
	cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
}
int main () {
	tollbooth toll (0, 0);
	toll.display();
}

Last edited on
thanks for the help both of you but duman i actually need those other functions for later thanks for helping though ill use what you gave me and see if i can add the functions back and make it work
@mikeal200 let me re-add them and use. You'll completely understand what's going on.
Here is the first version on CodeBox 1. They're included but still being called inside void tollbooth::display () I also made another version which you can use them completely separately. On the 2nd box, you have to call each time in main() you want to add a car.

1st one is more practical, however, 2nd is a solution too. Pick whatever works for you best.

If you have any further questions, do ask.

Edit: Fixed simple mistake. Works perfect all of them. :)

1stBox Output: (for input: p p n p q)
1
2
3
4
5
6
7
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> n
P - paid N - Not paid Q - Quit -> p
P - paid N - Not paid Q - Quit -> q
Total number of cars 4
Total amount collected $1.5



2ndBox Output:
1
2
Total number of cars 4
Total amount collected $1.5


CodeBox 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

class tollbooth {
	public:
		tollbooth (int, double);
		void payincar(); //increments the car total and adds .50 to the cash total
		void nopaycar(); //increments the car total but adds no money
		int display();

	private:
		int cars; //total cars
		double money; //total money made 
};
tollbooth::tollbooth(int a, double b){
	money = b;
	cars = a;
}
void tollbooth::payincar(){
	++cars;
	money += 0.5;
}
void tollbooth::nopaycar(){
	++cars;
}

int tollbooth::display(){
	string answer;
	while (answer != "q"){
		cout << "P - paid N - Not paid Q - Quit -> ";
		cin >> answer;

		if(answer == "q"){ break; }
		if(answer == "p"){ tollbooth::payincar(); }
		if(answer == "n"){ tollbooth::nopaycar(); }
	}
	cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
}
int main () {
	tollbooth toll (0, 0); // inits cars and money vars to 0
	toll.display();
}


CodeBox 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

class tollbooth {
	public:
		tollbooth (int, double);
		void payincar(); //increments the car total and adds .50 to the cash total
		void nopaycar(); //increments the car total but adds no money
		int display();

	private:
		int cars; //total cars
		double money; //total money made 
};
tollbooth::tollbooth(int a, double b){
	money = b;
	cars = a;
}
void tollbooth::payincar(){
	++cars;
	money = money + .50;
}
void tollbooth::nopaycar(){
	++cars;
}
int tollbooth::display(){
	cout << "Total number of cars " << cars << endl;
	cout << "Total amount collected $" << money << endl;
}
int main () {
	tollbooth toll (0, 0); // inits cars and money vars to 0
	toll.payincar();
	toll.payincar();
	toll.nopaycar();
	toll.payincar();
	toll.display();
}
Last edited on
#include <iostream>
#include <fstream>

using namespace std;

class tollbooth {
public:
tollbooth (int,double,string); // inits cars and money vars to 0
payincar (); //increments the car total and adds .50 to the cash total
nopaycar (); //increments the car total but adds no money
//display (ostream& fileout);
int getCars();
double getMoney();
private:
int cars; //total cars
double money; //total money made
string answer; //this is the answer the user inputs so either p, n, or q
};
tollbooth::tollbooth(int c=0,double m=0,string a="") {
cars=c;
money=m;
answer=a;
}
tollbooth::payincar(){
cars++;
money+=0.5;
}
tollbooth::nopaycar(){
cars++;
}
int tollbooth::getCars(){
return cars;
}
double tollbooth::getMoney(){
return money;
}
int main () {
tollbooth tool;
string answer,P,Q,N;

cout << "P - paid N - Not paid Q - Quit ->" << endl;
cin >> answer;
if(answer=="Q")
goto mylable;
if(answer=="P")
tool.payincar();
else if(answer=="N")
tool.nopaycar();
cout<<"cars="<<tool.getCars()<<endl;
cout<<"money="<<tool.getMoney();
mylable:
return 0;

}
thank you all to those that helped it was greatly appreciated and yes duman i do see what i was doing wrong thanks for helping and not just putting me down and saying i was dumb
@mikeal200 I'm glad that I did help you. No one is dumb. :)
Topic archived. No new replies allowed.
Pages: 12