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