Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Quene display

Reply
Thread Tools

Quene display

 
 
Kay
Guest
Posts: n/a
 
      09-01-2004
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;
}

 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      09-01-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-01-2004

"Kay" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display byte array back to pdf and display in IE SG Java 6 01-15-2008 01:02 PM
Quene Tuvas Python 2 11-30-2005 06:30 PM
choosing to display or not to display a checkbox in repeater control. Imran Aziz ASP .Net 4 08-11-2005 05:00 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
Display and display model Marc Twain Java 1 01-13-2004 07:05 PM



Advertisments