Increment in Variable not taking place...

I have created this code; it is a taxi management system. I've made a class 'List' which helps handle a linked list structure of the waiting taxis and waiting passengers. The class has a public int variable: waiting, which keeps track of the number of waiting taxis/passengers.

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>

using namespace std;

class Queue{ //Class for indivdual taxis and passengers, which will be members of linked list.
    public:
    string id;
    Queue *next;
    Queue (){
        id = "0";
        next = 0;}
    };

class List{ //Class for implementing linked list, will keep track of first and last members of list, and total number of members.
    public:
    Queue *first, *last;
    int waiting;
    List(){
        first=0; last=0;
        waiting=0;}
    };

int operator + (List l, string input){ //Binary operator overloading, used for inserting new members into linked list.
    Queue *tmp;
    tmp = new (nothrow) Queue;
    if (tmp==0) cout << "Error: memory could not be allocated."; //In case there is not enough heap memory.
    else{
        tmp->id = input; tmp->next=0; //Adds next member to
        if(l.waiting==0){
            l.first=tmp;
            l.last=tmp;
            l.waiting++;
        }
        else{
            l.last->next=tmp; l.last=tmp; //end of list.
            l.waiting++; //Increases number of waiting members by one.
        }
    }
    return 0;
    };

int operator-- (List l, int i){
    Queue *tmp;
    tmp = l.first->next; //removes first
    l.first = tmp; //member.
    l.waiting--; //Decreases number of waiting members by one.

    return 0;
    };


int main(){
    Queue taxi, passenger;
    List taxi_list, passenger_list;

    cout << "Type in 't' to enter a new taxi into the waiting list." << endl;
    cout << "Type in 'p' to enter a new passenger's name." << endl;
    cout << "Type in 'u' to enter many new taxis at once" << endl;
    cout << "Type in 'x' to exit the program";
    cout << endl;

    while (true){
        char command;
        cout << "What would you like to do?" << endl;
        cin >> command;
        if (command == 't'){
            string reg_no;
            cout << "Type in taxi registration number: ";
            cin >> reg_no;
            taxi_list + reg_no;
            cout << "taxi " << reg_no << " added to list" << endl << endl << taxi_list.waiting;
        }
        else if (command == 'p'){
            string passenger;
            cout << "type in passenger name: ";
            cin >> passenger;
            passenger_list + passenger;
            if (taxi_list.waiting==0) cout << "No Taxi available, " << passenger << " has been put on waiting list." << endl << endl;
        }
        else if (command == 'u'){
            int number;
            cout << "How many taxis would you like to add to the waiting list?";
            cin >> number;
            string taxis[number]; //creates array, in which taxi reg. no.s will be inputted
            cout << "Enter the taxis' registration numbers' one-by-one: " << endl;;
            for (int a=0; a<number; a++){
                cin >> taxis[a]; //takes taxi reg. no.s
                taxi_list + taxis[a]; //and adds them to list.
                cout << "taxi " << taxis[a] << " has been added to list." << endl;
            }
            cout << endl;
        }
        else if (command == 'x') break;

        if((passenger_list.waiting!=0)&&(taxi_list.waiting!=0)){
        cout << passenger_list.first->id << " has been assigned taxi " << taxi_list.first->id;
        passenger_list--;
        taxi_list--;}
    }

    return 0;
}


But when I run the code and type in a new taxi number, the code does not increment the number of taxis by one the linked list by one. I can't seem to find the problem. Please help.

BTW, in the 'int main', I've added "<< taxi_list.waiting" at the end of the line after the user inputs the new taxi registration number, so that I can see how many taxis are now on the list. This is what is being shown as zero, no matter what.
Last edited on
int operator + (List l, string input){ //Binary operator overloading, used for inserting new members into linked list.

You take the list by value, so any changes made to the copy you have in the overloaded operator will be lost.

Typically one wouldn't expect operator + to modify its parameters. It should return a new List consisting of the list fed to it plus the string fed to it.

operator+= might be more appropriate.
So, I had to call the list by reference. I fixed the problem by doing this:int operator += (List& l, string input). and similarly in the '--' operator.

And the use of the '+=' operator does feel a lot more natural.

Thanks!
Topic archived. No new replies allowed.