Quene display

K

Kay

This function is used to display the context of a queue. I input an
integer comparing with the queue cuisine number, if cuisine number is
same as the input, it shows the context. However, it cannot do what I
want. Did I do sth wrong ?

void DisplayRestaurantByCuisine( Queue * q, char input){

QueueNode * tmp;
char * item, * cuisine, *mode;

cout << "In restaurant by cuisine";
while(!QueueEmpty(q)){
tmp = q->front;
item = tmp->item;
cuisine = tmp->cuisine;
mode = tmp->mode;

if( input == cuisine ){ <-----------comparing
cout << item << mode << endl;
}
q->front = tmp->next;
}
 
K

Karl Heinz Buchegger

Kay said:
This function is used to display the context of a queue. I input an
integer comparing with the queue cuisine number, if cuisine number is
same as the input, it shows the context. However, it cannot do what I
want. Did I do sth wrong ?

void DisplayRestaurantByCuisine( Queue * q, char input){

QueueNode * tmp;
char * item, * cuisine, *mode;

cout << "In restaurant by cuisine";
while(!QueueEmpty(q)){
tmp = q->front;
item = tmp->item;
cuisine = tmp->cuisine;
mode = tmp->mode;

if( input == cuisine ){ <-----------comparing
cout << item << mode << endl;
}
q->front = tmp->next;
}

Aehm. This function modifies the queue by assigning something
to q->front. That is unusal for a display function. The
purpose of a display function is to display, nothing more.

The comparsion seems to be suspect (but I can't tell this
for sure, since you always just present code snippets without
context):

input denotes a single character
cuisine is a pointer to chararacter

So the above should not even compile due to data type
missmatch.
So if I assume that you retyped your code, instead of
cut&paste-ing the original code, and input in reality is

void DisplayRestaurantByCuisine( Queue * q, char* input){

(note: I changed 'char input' to 'char* input)

then

if( input == cuisine ){

does not do what you want: It compares 2 pointers and not
what the pointers point to. Comparing C-style strings is
done with a helper function strcmp

if( strcmp( input, cuisine ) == 0 ){

That leaves us with your looping construct. There is a funny
thing: A display function that loops as long as the data structure
is not empty.
Well. A display function has to display and leave the original
data structure as it is. But that also means: If the queue was
empty when the function starts, it stays empty. If the queue
was not empty, then just be outputting that queue it should
not become empty.


void DisplayRestaurantByCuisine( Queue * q, char* input)
{
QueueNode * tmp;
char * item, * cuisine, *mode;

cout << "In restaurant by cuisine";

tmp = q->front;

while( tmp != 0 ){
item = tmp->item;
cuisine = tmp->cuisine;
mode = tmp->mode;

if( strcmp( input, cuisine ) == 0 ){
cout << item << mode << endl;
}

tmp = tmp->next;
}
}


Don't get me wrong. But I think you are fighting too much right
now: You are fighting with very basic C knowledge and handling
dynamic data structures. This is most likely bound to fail with
a loud crash. Start simpler. Start with the C basics (or since
this is C++, start with C++ basics and throw C-style strings
out of the window). In doing so a good book as an absolute
must. See the FAQ for recommendations.
Only after you got fluent in basic C or C++, start with working
on dynamic data structures. Things like the above (comparing
pointers when you want to compare strings) *must* not happen when
you are tackling dynamic data structures. You can't study calculus
when you have problems with basic equation rearrangement.
 
M

Mike Wahler

Kay said:
This function is used to display the context of a queue. I input an
integer comparing with the queue cuisine number, if cuisine number is
same as the input, it shows the context. However, it cannot do what I
want. Did I do sth wrong ?

void DisplayRestaurantByCuisine( Queue * q, char input){

QueueNode * tmp;
char * item, * cuisine, *mode;

cout << "In restaurant by cuisine";
while(!QueueEmpty(q)){
tmp = q->front;
item = tmp->item;
cuisine = tmp->cuisine;
mode = tmp->mode;

if( input == cuisine ){ <-----------comparing

This attempts to compare a pointer with a character.
The language does not define such a thing.

If you want to compare the character 'input' with
the one pointed to by the pointer 'cuisine', then
dereference the pointer to get at the character
it points to.

if(input == *cuisine)

cout << item << mode << endl;
}
q->front = tmp->next;
}

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top