Quene display

Discussion in 'C++' started by Kay, Sep 1, 2004.

  1. Kay

    Kay Guest

    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;
    }
     
    Kay, Sep 1, 2004
    #1
    1. Advertising

  2. 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
     
    Karl Heinz Buchegger, Sep 1, 2004
    #2
    1. Advertising

  3. Kay

    Mike Wahler Guest

    "Kay" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Sep 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imran Aziz
    Replies:
    4
    Views:
    1,516
    Imran Aziz
    Aug 11, 2005
  2. Marc Twain

    Display and display model

    Marc Twain, Jan 13, 2004, in forum: Java
    Replies:
    1
    Views:
    382
    Matt Humphrey
    Jan 13, 2004
  3. SG
    Replies:
    6
    Views:
    30,411
    ricuzzo
    Jan 15, 2008
  4. blank101
    Replies:
    2
    Views:
    536
    blank101
    Feb 5, 2005
  5. Tuvas

    Quene

    Tuvas, Nov 30, 2005, in forum: Python
    Replies:
    2
    Views:
    531
    Peter Hansen
    Nov 30, 2005
Loading...

Share This Page