It is a taxi dispatch problem could anyone help with Logical errrors !

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
#include<iostream>
using namespace std;


struct Queue{ 

    int nwaiting,Front;// nwaiting is the no. of drivers waiting . 
                       // Front is the driver no. (not id) at front .
                       // 0<=nwaiting,Front<=10 

    int elements[10];  //defining an array to for driver IDs .

    Queue(){
        nwaiting=0;
        Front=0;
    }

    bool Insert(int driverID){         // while a driver comes insert f^n would 
                                        //insert insert him in the Queue & tell 
                                         //whether it is possible to insert him 
                                          //or not .
        if(nwaiting==10) return false;
        else {
              elements[(Front+nwaiting)%10]=driverID;
              nwaiting++;
              return true;
             }
    }

    bool Remove(int &driverID){       //when a customer comes Remove f^n would 
                                      //remove the front driver from the Queue
                                      // And also tell whether a driver is 
                                      // available or not .
        if(nwaiting==0) return false;
        else {
              driverID=elements[Front];
              Front=(Front+1)%10;
              nwaiting--;
              return true;
             }
    }

};

int main(){

    Queue Q;

    while(true){
        char command;
        cin>>command;

        if(command=='d'){
            int ID;
            cout<<"Enter your driverID: "<<endl;
            cin>>ID;

            if(Q.Insert(ID)==false){
                cout<<"Sorry no vacancies !"<<endl;
            }

        }

        if(command=='c'){
            int *p= new int;

            if(Q.Remove(*p)){
                cout<<"Sorry, no cabs available ."<<endl;
                                }
            else cout<<"You are assigned driver: "<<*p<<endl;

            delete p;
        }

    }
}
Hell Avi0110,

So what is it doing, not doing and what is expected?

Having no idea what the program should do or what is expected from the program this may take a while.

If you describe your problem better you could get a quicker answer.

Andy
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
#include<iostream>
using namespace std;


struct Queue {

    int nwaiting, Front;// nwaiting is the no. of drivers waiting . 
                       // Front is the driver no. (not id) at front .
                       // 0<=nwaiting,Front<=10 

    int elements[10];  //defining an array to for driver IDs .

    Queue()
        : nwaiting(0), Front(0), elements{} {}

    bool Insert(int driverID) {         // while a driver comes insert f^n would 
                                        //insert insert him in the Queue & tell 
                                         //whether it is possible to insert him 
                                          //or not .
        if (nwaiting > 8) return false;
        else {
            elements[nwaiting] = driverID;
            nwaiting++;
            return true;
        }
    }

    bool Remove(int* driverID) {             
        if (nwaiting-1 < 0) return false;
        else {
            *driverID = elements[nwaiting-1];
            elements[nwaiting-1] = 0;
            nwaiting--;
            return true;
        }
    }

};

int main() {

    Queue Q;
    while (true) {
        string command = "";
        cout << "Enter Command d or c?"<<"\n";
        cin >> command;

        if (command == "d") {
            int ID = 0;
            cout << "Enter your driverID: " << endl;
            cin >> ID;

            if (Q.Insert(ID) == false) {       
                cout << "Sorry no vacancies !" << endl;
            }

        }

        if (command == "c") {
            int* p = new int;

            if (!Q.Remove(p)) {
                cout << "Sorry, no cabs available ." << endl;
            }
            else cout << "You are assigned driver: " << *p << endl;

            delete p;
        }
    }
}


its good
Last edited on
Hey marky , I wanna ask sth from you !

1
2
3
 Queue()
        : nwaiting(0), Front(0), **elements{}** 
 {}


You are assigning the arrays of elements with the starred statement , what it actually does ??

*elements{}*

Thanks andy for the advise .
I myself was wondering why i am not getting any replies : ) .

Actually the problem was that there wasn't any syntax error .
I was able to run my program but

When i was entering the 'c' as command here (51st line):
1
2
3
while(true){
        char command;
        cin>>command;


I was getting some garbage value instead of the message "Sorry, no cabs available ."

Would you pls clarify what i am doing wrong here in these two codes:
1
2
3
4
5
6
7
8
9
10
        if(command=='c'){
            int *p= new int;

            if(Q.Remove(*p)){
                cout<<"Sorry, no cabs available ."<<endl;
                                }
            else cout<<"You are assigned driver: "<<*p<<endl;

            delete p;
        }



1
2
3
4
5
6
7
8
9
   bool Remove(int* driverID) {             
        if (nwaiting-1 < 0) return false;
        else {
            *driverID = elements[nwaiting-1];
            elements[nwaiting-1] = 0;
            nwaiting--;
            return true;
        }
    }
Hello Avi011,

Looking at the if statement in "main". Line 2, Is there a reason for creating dynamic memory here? int driverID{}; works just the same with less work. At the function you will be passing the variable by reference.

At line 4 think about what is returned by the function. Should the function find no cabs available it returns false. The if statement evaluates to false, so you go to the "else" part. There, in the "cout", of the else part "driverID" has no value except the garbage that is in the memory set aside for the variable.

In the end the variable is never initialized or does it receive a value in the function. The if statement is not using the returned value properly, so you print out thee wrong message.

I solved the problem with if(!Q.Remove(driverID)). Now the (!) at the beginning of the function call will flip the returned value and use the if/else correctly. This was easier than reworking the function.

I will say this in case you do not realize it:
1
2
3
4
5
6
7
8
9
10
11
else if (command == 'd')
{
	int driverID{};

	if (!Q.Remove(driverID))
	{
		cout << "\n   Sorry, no cabs available ." << endl;
	}
	else
		cout << "\n  You are assigned driver: " << driverID << endl;
}

"driverID" is a local variable to the if statement when the closing } is reached the variable is destroyed.

For the "remove" function I am not sure how it is working because I have not tested it yet. I can say that the original function is working just fine. I am not sure why you thought you had to change it unless you are trying to fix the problem in the wrong place.

This is what I came up with from your original code:
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
103
104
105
106
107
108
#include <cctype>
#include <iostream>
#include <limits>

using namespace std;

constexpr int MAXSIZE{ 5 };  // <--- Change value as needed. Set to 5 for testing.

struct Queue
{
	int nwaiting{}, Front{};  // nwaiting is the no. of drivers waiting . 
							  // Front is the driver no. (not id) at front.
							  // 0<=nwaiting,Front<=10.

	int elements[MAXSIZE]{};  //defining an array to for driver IDs .

	//Queue()  // <--- Not needed. Variables can be initialized when first defined.
	//{
	//	nwaiting = 0;
	//	Front = 0;
	//}

	bool Insert(int driverID)
	{          // while a driver comes insert f^n would 
			   // insert insert him in the Queue & tell 
			   // whether it is possible to insert him 
			   // or not .
		if (nwaiting == MAXSIZE)
			return false;
		else
		{
			elements[(Front + nwaiting) % MAXSIZE] = driverID;
			nwaiting++;
			return true;
		}
	}

	bool Remove(int &driverID)
	{       //when a customer comes Remove f^n would 
			//remove the front driver from the Queue
			// And also tell whether a driver is 
			// available or not .

		if (nwaiting == 0)
			return false;
		else
		{
			driverID = elements[Front];

			Front = (Front + 1) % MAXSIZE;

			nwaiting--;

			return true;
		}
	}
};

int main()
{
	Queue Q;

	while (true)
	{
		char command;

		cout <<
			"\n a. Add"
			"\n d. Dispatch"
			"\n e. Exit? "
			"\n Enter Command: ";
		cin >> command;
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		command = std::tolower(command);

		if (command == 'a')
		{
			int ID;
			cout << "\n Enter your driverID: ";
			cin >> ID;

			if (!Q.Insert(ID))
			{
				cout << "\n     Sorry no vacancies !" << endl;
			}
		}
		else if (command == 'd')
		{
			int driverID{};

			if (!Q.Remove(driverID))
			{
				cout << "\n   Sorry, no cabs available ." << endl;
			}
			else
				cout << "\n  You are assigned driver: " << driverID << endl;
		}
		else if (command == 'e')
			break;
		else
		{
			std::cout << "\n     Invalid choice! Try again.\n";
		}
	}

	return 0;  // <--- Not required, but makes a good break point.
}

I changed some parts around to make the output on the screen look better. Except for the extras I added there was not that much I had to change.

I added line 73 in case you type more than 1 character. I clears the input buffer.

Andy

You are assigning the arrays of elements with the starred statement , what it actually does ??

*elements{}*



Idk where you see this... i didn't put that in my code. What I posted was fully functional and tested repeatedly. Thats kinda why I didn't come back.

1
2
3

bool Remove(int* driverID) { if (nwaiting-1 < 0) return false; 
else { *driverID = elements[nwaiting-1]; elements[nwaiting-1] = 0; nwaiting--; return true; } }


I forget why I did this. Probably bc you put in a pointer as the parameter. Figured it made more sense for the variables to match and just dereference it once in the function idk. The whole thing was unnecessary. Could have just been an int passed by reference.
1
2
Queue() : nwaiting(0), Front(0), elements{} {}


This is just an initializer list. Visual studio complains if you don't do this with member variables.
Last edited on
Topic archived. No new replies allowed.