Need a little Help Please

This is a discussion on Need a little Help Please within the C Programming forums, part of the General Programming Boards category; Getting two errors and warning when compiling this code any help would be great. Code: #include <stdio.h> #define SONG "Say ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Unhappy Need a little Help Please

    Getting two errors and warning when compiling this code any help would be great.

    Code:
    #include <stdio.h>
    #define SONG  "Say It Aint So"
    #define SHOW  "Survivorman"
    int menu (void);
    void song (void);
    void show (void);
    int QUIT(void);
    int main(void)
       {
       char choice;
       choice = menu();   /* get user's first selection */
    
       while(choice != QUIT)
       {
          switch(choice)
          {
             case SHOW:  show();
                         break;
             case SONG:  song();
                         break;
             default:    printf("Oops!  An invalid choice slipped through.  ");
                         printf("Please try again.\n");
          }
          choice = menu();  /* get user's subsequent selections */
       }
    
       printf("Bye bye!\n");
    
       /* These next 3 lines are helpful if your program doesn't pause to let you
          see the output.  (Not required.)
       */
       printf("Press Enter to end the program.\n");
       fflush(stdin);
       getchar();
    
       return 0;
    }
    
    int menu(void)
    {
       int option;
    
           printf("Learn about me! Please select from the menu\n\n");
           printf("1. Learn the name of my favourite show\n");
           printf("2. Learn the first line of my favorite song\n\n");
           printf("0. Quit this program\n\n");
           printf("Enter your choice: ");
    
       while( (scanf(" %d", &option) != 1)  /* non-numeric input */
              || (option < 0)               /* number too small */
              || (option > 2))              /* number too large */
       {
          fflush(stdin);                    /* clear bad data from buffer */
          printf("That selection isn't valid.  Please try again.\n");
          printf("Your choice?  ");
       }
       return option;
    }
    
    void song(void)
    {
       printf(" %d\n", SONG);
    }
    
    void show(void)
    {
       printf(" %d\n", SHOW);
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You'll get a lot more help if you actually post the error messages that you get.

    Besides, with my warning level turned up, I get a lot more than two errors and warnings.
    Code:
    twoerrors.c: In function ‘main’:
    twoerrors.c:13: warning: comparison between pointer and integer
    twoerrors.c:17: error: case label does not reduce to an integer constant
    twoerrors.c:19: error: case label does not reduce to an integer constant
    twoerrors.c: In function ‘song’:
    twoerrors.c:62: warning: format ‘&#37;d’ expects type ‘int’, but argument 2 has type ‘char *’
    twoerrors.c: In function ‘show’:
    twoerrors.c:67: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
    The main thing is that you have a fundamental problem with your switch statement. Case label values can only be integers. They cannot be pointers, strings, or floating-point numbers.

    Oh yes, and don't use fflush(stdin), it's non-standard. See the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    fflush(stdin); /* clear bad data from buffer */
    see faq
    while(choice != QUIT)
    QUIT is a function pointer.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by Mike1982
    Getting two errors and warning when compiling this code any help would be great.
    Well... what are they? Are we supposed to guess?

    Code:
    int QUIT(void);
    int main(void)
       {
       char choice;
       choice = menu();   /* get user's first selection */
    
       while(choice != QUIT)
    QUIT is a function, when you use it in your code you must make a call to the function... that requires the use of parenthesis:
    Code:
    while(choice != QUIT())


    Code:
    #define SONG  "Say It Aint So"
    #define SHOW  "Survivorman"
    
    ...
    
    void song(void)
    {
       printf(" %d\n", SONG);
    }
    
    void show(void)
    {
       printf(" %d\n", SHOW);
    }
    Perhaps you meant %s which is for strings? %d is for use in displaying integers. Also...
    Code:
    case SHOW:  show();
        break;
    case SONG:  song();
    Those cannot be in a case statement, only integral types are allowed, not string literals.


    Code:
    fflush(stdin);
    fflush of input streams is undefined. There are other ways to clear the input buffer. Check the FAQ.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Sorry about that guys The warning and errors I am getting are

    Code:
    case label dose not reduce to an integer constant
    Code:
    case label dose not reduce to an integer constant
    Code:
    warning: comparison between pointer and interger
    Code:
    warning: unreachable code at beginning of switch statement
    Code:
    #include <stdio.h>
    #define SONG  "Say It Aint So"
    #define SHOW  "Survivorman"
    int menu (void);
    void song (void);
    void show (void);
    int QUIT(void);
    int main(void)
       {
       char choice;
       choice = menu();   /* get user's first selection */
    
       while(choice != QUIT)
       {
          switch(choice)
          {
             case SHOW:  show();
                         break;
             case SONG:  song();
                         break;
             default:    printf("Oops!  An invalid choice slipped through.  ");
                         printf("Please try again.\n");
          }
          choice = menu();  /* get user's subsequent selections */
       }
    
       printf("Bye bye!\n");
    
       /* These next 3 lines are helpful if your program doesn't pause to let you
          see the output.  (Not required.)
       */
       printf("Press Enter to end the program.\n");
       fflush(stdin);
       getchar();
    
       return 0;
    }
    
    int menu(void)
    {
       int option;
    
           printf("Learn about me! Please select from the menu\n\n");
           printf("1. Learn the name of my favourite show\n");
           printf("2. Learn the first line of my favorite song\n\n");
           printf("0. Quit this program\n\n");
           printf("Enter your choice: ");
    
       while( (scanf(" %d", &option) != 1)  /* non-numeric input */
              || (option < 0)               /* number too small */
              || (option > 2))              /* number too large */
       {
       }
       return option;
    }
    
    void song(void)
    {
       printf(" %s\n", SONG);
    }
    
    void show(void)
    {
       printf(" %s\n", SHOW);
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so a "case label" is what's after case in a switch statement - and it needs to be something that can be expressed as an integer. Do you think "Say It Aint So" is an integer?

    "warning: comparison between pointer and interger" - this is fairly obvious, except that you probably don't realize that "choice != QUIT" is a comparison to an pointer... QUIT is a function, and when it's not having parenthesis after it, it means that you want to take the address of (pointer to) the function - and that shouldn't be compared with an integer, according to the compiler [and I think the compiler is right in this case].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Ya I think I have to re think this whole thing and start from scratch thanks for everyones help

    Quote Originally Posted by matsp View Post
    Yes, so a "case label" is what's after case in a switch statement - and it needs to be something that can be expressed as an integer. Do you think "Say It Aint So" is an integer?

    "warning: comparison between pointer and interger" - this is fairly obvious, except that you probably don't realize that "choice != QUIT" is a comparison to an pointer... QUIT is a function, and when it's not having parenthesis after it, it means that you want to take the address of (pointer to) the function - and that shouldn't be compared with an integer, according to the compiler [and I think the compiler is right in this case].

    --
    Mats

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In other words, SHOW and SONG need to be 1 and 2, respectively, because that's what the user will be entering. Then you probably want some separate #defines for the messages, which will be printed in song() and show().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Thanks dwks that helps I am starting to get it

    Quote Originally Posted by dwks View Post
    In other words, SHOW and SONG need to be 1 and 2, respectively, because that's what the user will be entering. Then you probably want some separate #defines for the messages, which will be printed in song() and show().

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Ok it complies now but crashes when I run it I guess compliers don't know everything I can screw up lol

    Code:
    #include <stdio.h>
    #define SONG  1
    #define SHOW  2
    #define QUIT  0
    int menu (void);
    void song (void);
    void show (void);
    int main(void)
       {
       char choice;
       choice = menu();   /* get user's first selection */
    
       while(choice != QUIT)
       {
          switch(choice)
          {
             case SHOW:  show();
                         break;
             case SONG:  song();
                         break;
             default:    printf("Oops!  An invalid choice slipped through.  ");
                         printf("Please try again.\n");
          }
          choice = menu();  /* get user's subsequent selections */
       }
    
       printf("Bye bye!\n");
    
    
    
       return 0;
    }
    
    int menu(void)
    {
       int option;
    
           printf("Learn about me! Please select from the menu\n\n");
           printf("1. Learn the name of my favourite show\n");
           printf("2. Learn the first line of my favorite song\n\n");
           printf("0. Quit this program\n\n");
           printf("Enter your choice: ");
    
       while( (scanf(" &#37;d", &option) != 1)  /* non-numeric input */
              || (option < 0)               /* number too small */
              || (option > 2))              /* number too large */
    
       return option;
    }
    
    void song(void)
    {
       printf(" %s\n", SONG);
    }
    
    void show(void)
    {
       printf(" %s\n", SHOW);
    }
    Last edited by Mike1982; 10-10-2007 at 03:05 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe you want to use a different #define name for your "I like this song" part? Trying to printf a string with a constant of 2 isn't going to work.

    Also, if you add -Wall to your line when you compile, it will give you more info about things that you may be doing wrong (in this case, it will say something like "format %s expects a char *, but given integer".

    No, compilers don't discover ALL the stupid things we can do - only some of the easier to determine and common mistakes [and many less common mistakes - but only because the compiler can't cope, rather than because it's "wrong"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    void song(void)
    {
       printf(" &#37;s\n", SONG);
    }
    
    void show(void)
    {
       printf(" %s\n", SHOW);
    }
    SONG and SHOW are integers. You need some separate messages. I'd hard-code the messages in song() and show() -- you're already hard-coding the function calls.

    BTW, your compiler should warn you about this. Mine does. I'd turn up the warning level if I were you. Are you using Dev-C++?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Yes using Dev-C++. By hard-code do you mean add define statements for 'song'
    and 'show'

    Thanks for all the help

    Quote Originally Posted by dwks View Post
    Code:
    void song(void)
    {
       printf(" &#37;s\n", SONG);
    }
    
    void show(void)
    {
       printf(" %s\n", SHOW);
    }
    SONG and SHOW are integers. You need some separate messages. I'd hard-code the messages in song() and show() -- you're already hard-coding the function calls.

    BTW, your compiler should warn you about this. Mine does. I'd turn up the warning level if I were you. Are you using Dev-C++?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I meant that you could probably get away with
    Code:
    void song(void)
    {
       printf(" Say It Aint So\n");
    }
    
    void show(void)
    {
       printf(" Survivorman\n");
    }
    But you could, of course, create separate #defines for the messages. SONG_TEXT and SHOW_TEXT, say.

    In Dev-C++, go to Compiler Options and add -W -Wall to the extra compiler options text dialog. You'll get a lot more warnings that way.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Ah ok thanks I think I will use #defines. I will also try to figure out how to turn up warning levels in Dev-C++. I will post my results

    Thanks

    Quote Originally Posted by dwks View Post
    I meant that you could probably get away with
    Code:
    void song(void)
    {
       printf(" Say It Aint So\n");
    }
    
    void show(void)
    {
       printf(" Survivorman\n");
    }
    But you could, of course, create separate #defines for the messages. SONG_TEXT and SHOW_TEXT, say.

    In Dev-C++, go to Compiler Options and add -W -Wall to the extra compiler options text dialog. You'll get a lot more warnings that way.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21