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
#include <iostream>
#include <fstream>

using namespace std;

class tollbooth {
	public:
		tollbooth (); // 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);

	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 :: payincar () {
	if (answer = "p") {
		++cars;
		money = money + .50;
	}
}
int main () {
	cout << "P - paid N - Not paid Q - Quit ->" << endl;
	cin >> answer;
}

heres the error C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc: In member function 'int tollbooth::payincar(std::__cxx11::string)':
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:18:13: error: could not convert 'answer.std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"p"))' from 'std::__cxx11::basic_string<char>' to 'bool'
if (answer = "p") {
~~~~~~~^~~~~
i think i just need to change the type of answer
Last edited on
i have also tried changing line 19 which is where the error occurs i have tried changing it to if (answer = 'p') {
Remember that = is the assignment operator, == is used in comparisons.

Also you should have several other error messages which are probably related to the error you reported:

9:13: error: ISO C++ forbids declaration of 'payincar' with no type [-fpermissive] 18:24: error: ISO C++ forbids declaration of 'payincar' with no type [-fpermissive] In member function 'int tollbooth::payincar()': 19:18: error: could not convert '((tollbooth*)this)->tollbooth::answer.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"p"))' from 'std::basic_string<char>' to 'bool' 23:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int main()': 26:9: error: 'answer' was not declared in this scope
so i have changed the program up a bit and fixed it but still get errors that i dont know what they mean but i never got the errors you said other than the one about int main not having answer i fixed that but would someone give it a look

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
#include <iostream>
#include <fstream>

using namespace std;

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

	private:
		int cars; //total cars
		double money; //total money made 
};
tollbooth :: payincar (string answer) {
	if (answer == "p") {
		++cars;
		money = money + .50;
	}
}
tollbooth :: nopaycar (string answer) {
	if (answer == "n") {
		++cars;
}
tollbooth :: display (ostream& fileout, 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;
}
int main () {
	string answer;//this is the answer the user inputs so either p, n, or q	
}


NPP_EXEC: "Compile C++ File"
NPP_SAVE: C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc
g++ -o "C:\Users\lords_000\Downloads\pocketcpp\lab12b" "C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc" -static -std=c++14
Process started >>>
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc: In member function 'int tollbooth::nopaycar(std::__cxx11::string)':
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:27:30: error: expected primary-expression before '&' token
 tollbooth :: display (ostream& fileout, string answer){
                              ^
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:27:32: error: 'fileout' was not declared in this scope
 tollbooth :: display (ostream& fileout, string answer){
                                ^~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:27:48: error: expected primary-expression before 'answer'
 tollbooth :: display (ostream& fileout, string answer){
                                                ^~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:37:13: error: a function-definition is not allowed here before '{' token
 int main () {
             ^
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:39:1: error: expected '}' at end of input
 }
 ^
<<< Process finished. (Exit code 1)
================ READY ================
i tried adding ostream fileout; to int main () {} but it didnt do anything
You're missing a closing } bracket.
Some functions are also missing return types and I think you'll find the compiler complaining about this line.

while (!answer = 'q');
i dont see where im missing a closing bracket i thought it was at that while (!answer = 'q'); but it isnt and whats wrong with that statement and also no the compiler doesnt complain about the functions without types because they do have types the class is the type right ??
Look at what the embedded compiler (the gear icon to the upper right of your source code) has to say about your code:

9:26: error: ISO C++ forbids declaration of 'payincar' with no type [-fpermissive]
10:26: error: ISO C++ forbids declaration of 'nopaycar' with no type [-fpermissive]
11:43: error: ISO C++ forbids declaration of 'display' with no type [-fpermissive]
17:37: error: ISO C++ forbids declaration of 'payincar' with no type [-fpermissive]
In member function 'int tollbooth::payincar(std::string)': 22:1: warning: no return statement in function returning non-void [-Wreturn-type]
At global scope: 23:37: error: ISO C++ forbids declaration of 'nopaycar' with no type [-fpermissive] In member function 'int tollbooth::nopaycar(std::string)': 27:30: error: expected primary-expression before '&' token
27:32: error: 'fileout' was not declared in this scope
27:48: error: expected primary-expression before 'answer'
37:13: error: a function-definition is not allowed here before '{' token 39:1: error: expected '}' at end of input
39:1: warning: no return statement in function returning non-void [-Wreturn-type]


So start with the first error on line 9: payincar (string answer); //increments the car total and adds .50 to the cash total
You must tell the compiler what this function is going to be returning. For example void, int, double. (void payincar(string answer);).

Your member functions must also have a return type: void tollbooth::payincar (string answer) { and your definition and function must both agree with the return type.

Once you fix all the member function definitions and functions you will still have at least one missing ending brace '}' for one of your member function definitions.

Lastly you're still trying to use the assignment operator like it was the comparison operator:
while (!answer = 'q');
There are a couple of things wrong here. First answer is a string, the compiler won't know how to negate a string (!answer). The second thing wrong is that your using the assignment operator= when you should be using the comparison operator==. The last thing wrong is that you're trying to assign (or compare) a string to a character const ('q'). In C++ character constants use the single quotation marks ' ' and a string constant uses double quotation marks " ". So this line should look more like: while(answer != "q"); and don't forget that C++ is case sensitive "q" is not the same as "Q".


thanks man ill let you know how it goes
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
#include <iostream>

using namespace std;

class tollbooth {
	public:
		tollbooth (); // 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
		void display (string answer);

	private:
		int cars; //total cars
		double money; //total money made 
};
void tollbooth :: payincar (string answer) {
	if (answer == "p") {
		++cars;
		money = money + .50;
	}
}
void tollbooth :: nopaycar (string answer) {
	if (answer == "n") {
		++cars;
	}
}
void 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;
}
int main () {
	string answer;//this is the answer the user inputs so either p, n, or q	
}

ok so this compiles but does nothing when i run it
A class definition is sort of a blueprint for the object. The code hasn't actually created or instantiated an actual tollbooth object yet. Also - there's a constructor declared at line 7, but there's no definition for it yet.

In main, you can create an object of type tollbooth then call functions on the created tollbooth. You might end up revisiting some of the logic in the member functions. Right now the display function will keep asking for answer, but not do anything besides output what was entered.

The user prompt calls for upper case letters, but the functions are looking for lower case. You might want to make either work.
yea im just going to use toupper for the lower case letters but thanks
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 (); // 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
		void display (string answer);

	private:
		int cars; //total cars
		double money; //total money made 
};
tollbooth :: tollbooth () {
	money = 0.0;
	cars = 0;
}
void tollbooth :: payincar (string answer) {
	if (answer == "p") {
		++cars;
		money = money + .50;
	}
}
void tollbooth :: nopaycar (string answer) {
	if (answer == "n") {
		++cars;
	}
}
void 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;
}
int main () {
	tollbooth toll;
	string answer;//this is the answer the user inputs so either p, n, or q	
}

so this is what i have now and still the program does nothing idk if im defining the constructor right
on line 42 ive also tried tollbooth toll (0, 0); or even just tollbooth toll (0); and it gives me errors
such as this is with the (0,0)
NPP_EXEC: "Compile C++ File"
NPP_SAVE: C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc
g++ -o "C:\Users\lords_000\Downloads\pocketcpp\lab12b" "C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc" -static -std=c++14
Process started >>>
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc: In function 'int main()':
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:42:19: error: no matching function for call to 'tollbooth::tollbooth(int)'
  tollbooth toll (0);
                   ^
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:16:1: note: candidate: tollbooth::tollbooth()
 tollbooth :: tollbooth () {
 ^~~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:16:1: note:   candidate expects 0 arguments, 1 provided
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:5:7: note: candidate: constexpr tollbooth::tollbooth(const tollbooth&)
 class tollbooth {
       ^~~~~~~~~
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:5:7: note:   no known conversion for argument 1 from 'int' to 'const tollbooth&'
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:5:7: note: candidate: constexpr tollbooth::tollbooth(tollbooth&&)
C:\Users\lords_000\Downloads\pocketcpp\lab12b.cc:5:7: note:   no known conversion for argument 1 from 'int' to 'tollbooth&&'
<<< Process finished. (Exit code 1)
================ READY ================

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); // 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
		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 :: payincar (string answer) {
	if (answer == "p") {
		++cars;
		money = money + .50;
	}
}
void tollbooth :: nopaycar (string answer) {
	if (answer == "n") {
		++cars;
	}
}
void 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;
}
int main () {
	tollbooth toll (0, 0);
	string answer;//this is the answer the user inputs so either p, n, or q	
}

ive tried this and this compiles but when i run it it just ends guess im not very good at this shit
You can define multiple constructors so that one can set to defaults, and another can take specific values for parameters.

So now that you've created a tollbooth object in main, try calling the display function on it.
Last edited on
i dont really understand what you mean call the function on it like do you mean toll.(something here) something like that or what im just having a hard time understanding what you mean
do you mean toll.(something here)


Right. The function needs to know which object it's operating on.

1
2
3
4
5
int main () {
	tollbooth toll (0, 0);
	string answer;//this is the answer the user inputs so either p, n, or q	
        toll.display(answer);
}
ok i get it so do i have to call all the functions?
Pages: 12