Help

Why is my program starting to count to 3 instead of 0?
I am doing for loops for every car that entered and count the money of paying cars but also count the cars in non paying cars but doesnt increase the total money. Sorry for my bad english..

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class tollbooth
{
private:
int cars,payingcar,nonpayingcar;
float money;

public:
void getPayingcar()
{
cars++;
money += 150;
//paying cars total and adds 150 pesos to the cash total
}
void getNonpayingcar()
{
cars++;
//non paying cars total BUT ADDS NOTHING to the cash total
}
void Display()
{
cout<<"Total Cars: "<<cars<<endl;
cout<<"Total Money: "<<money<<endl;
}
};

main()
{
tollbooth c;
int choice;
for (int i=1;i>0;i++)
{
cout<<"--------Tollbooth--------"<<endl;
cout<<"Please choose your choice"<<endl;
cout<<"1 - Count Paying cars"<<endl;
cout<<"2 - Count Non-paying cars"<<endl;
cout<<"3 - Print total cars and cash and exit"<<endl;
cout<<"Choice: ";
cin>>choice;
system("CLS");
switch(choice)
{
case 1:
c.getPayingcar();
break;
case 2:
c.getNonpayingcar();
break;
case 3:
c.Display();
return 0;
}
}
}
closed account (48T7M4Gy)
I'm guessing because it is not very clear what this is supposed to do. Howver this runs and produces a menu.

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
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

class tollbooth
{
private:
    int cars =0,payingcar = 0,nonpayingcar = 0; // <--
    float money = 0.0;// <--
    
public:
    void getPayingcar()
    {
        cars++;
        money += 150;
        //paying cars total and adds 150 pesos to the cash total
    }
    void getNonpayingcar()
    {
        cars++;
        //non paying cars total BUT ADDS NOTHING to the cash total
    }
    void Display()
    {
        cout<<"Total Cars: "<<cars<<endl;
        cout<<"Total Money: "<<money<<endl;
    }
};

int main() //<--
{
    tollbooth c;
    int choice;
    
    for (int i=1;i>0;i++)
    {
        cout<<"--------Tollbooth--------"<<endl;
        cout<<"Please choose your choice"<<endl;
        cout<<"1 - Count Paying cars"<<endl;
        cout<<"2 - Count Non-paying cars"<<endl;
        cout<<"3 - Print total cars and cash and exit"<<endl;
        cout<<"Choice: ";
        cin>>choice;
        system("CLS");
        switch(choice)
        {
            case 1:
                c.getPayingcar();
                break;
            case 2:
                c.getNonpayingcar();
                break;
            case 3:
                c.Display();
                break; //<--
        }
    }
    return 0; //<--
}
--------Tollbooth--------
Please choose your choice
1 - Count Paying cars
2 - Count Non-paying cars
3 - Print total cars and cash and exit
1
Choice: sh: CLS: command not found
--------Tollbooth--------
Please choose your choice
1 - Count Paying cars
2 - Count Non-paying cars
3 - Print total cars and cash and exit
3
Choice: sh: CLS: command not found
Total Cars: 1
Total Money: 150
--------Tollbooth--------
Please choose your choice
1 - Count Paying cars
2 - Count Non-paying cars
3 - Print total cars and cash and exit
2
Choice: sh: CLS: command not found
--------Tollbooth--------
Please choose your choice
1 - Count Paying cars
2 - Count Non-paying cars
3 - Print total cars and cash and exit
3
Choice: sh: CLS: command not found
Total Cars: 2
Total Money: 150
--------Tollbooth--------
Please choose your choice
1 - Count Paying cars
2 - Count Non-paying cars
3 - Print total cars and cash and exit
Program ended with exit code: 9
Last edited on
closed account (48T7M4Gy)
You also need to initialise all your variables.

Do that and you get some 'meaningful' output.
Last edited on
It says

[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
closed account (48T7M4Gy)
1
2
3
4
5
6
private:
    int cars,payingcar,nonpayingcar; // <--
    float money;// <--
    
public:
    tollbooth(){cars = 0; payingcar = 0; nonpayingcar = 0;}// <-- 


Warnings are often there to be ignored (despite the perils). With this warning you need to change your code as shown to include a constructor to overcome the problem or, better still use a better and up to date compiler.
Adding one of these two flags:
-std=c++11 or -std=gnu++11
to the compiler options won't help?
Topic archived. No new replies allowed.