By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Quene display

P: n/a
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;
}

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Kay wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a

"Kay" <er*********@yahoo.com.hk> wrote in message
news:41**************@yahoo.com.hk...
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
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.