Quene display

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kay

    #1

    Quene display

    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 DisplayRestaura ntByCuisine( Queue * q, char input){

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

    cout << "In restaurant by cuisine";
    while(!QueueEmp ty(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;
    }

  • Karl Heinz Buchegger

    #2
    Re: Quene display

    Kay wrote:[color=blue]
    >
    > 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 DisplayRestaura ntByCuisine( Queue * q, char input){
    >
    > QueueNode * tmp;
    > char * item, * cuisine, *mode;
    >
    > cout << "In restaurant by cuisine";
    > while(!QueueEmp ty(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;
    > }[/color]

    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 DisplayRestaura ntByCuisine( 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 DisplayRestaura ntByCuisine( 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
    kbuchegg@gascad .at

    Comment

    • Mike Wahler

      #3
      Re: Quene display


      "Kay" <ericjo92003@ya hoo.com.hk> wrote in message
      news:4135AA00.1 050506@yahoo.co m.hk...[color=blue]
      > 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 DisplayRestaura ntByCuisine( Queue * q, char input){
      >
      > QueueNode * tmp;
      > char * item, * cuisine, *mode;
      >
      > cout << "In restaurant by cuisine";
      > while(!QueueEmp ty(q)){
      > tmp = q->front;
      > item = tmp->item;
      > cuisine = tmp->cuisine;
      > mode = tmp->mode;
      >
      > if( input == cuisine ){ <-----------comparing[/color]

      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)

      [color=blue]
      > cout << item << mode << endl;
      > }
      > q->front = tmp->next;
      > }[/color]

      -Mike


      Comment

      Working...