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();
}
|