code help

This is a discussion on code help within the C++ Programming forums, part of the General Programming Boards category; Code: This is what I have so far #include "stdafx.h" #define QUIT 0 #define SHOW 1 #define SONG 2 int ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    code help

    Code:
    This is what I have so far
    
    #include "stdafx.h"
    #define QUIT 0
    #define SHOW 1
    #define SONG 2
    
    
    int menu(void) ;
    void song(void);
    void show(void);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
     int 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");
     
     
       printf("Press Enter to end the program.\n");
       fflush(stdin);
       getchar();
     
       return 0;
    }
    
    int menu(void) 
    {
       int option;
    
       printf("Learn more about me!\n");
       printf("Please select from the menu.\n");
       
       
       printf("Enter 1 to learn the name of my favourite show.\n");
       printf("Enter 2 to learn the first line of my favouite song.\n");
       
       printf("Enter 0 to quit this program.\n") ;  
       
       printf("Please enter your choice:.\n");
     
    
       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("Sunny days seem to hurt the most. \n");
    }
     
    void show(void)
    {
       printf("SportsCentre. \n");
    
    }
    
    
    The problem is when it runs and i enter 1 or 2 nothing appears even 0 to quit nothing. Ive been told that while("choice != QUIT"); is an error and my case statements are wrong but i have no idea how to fix them Thanks
    Last edited by Makdaddy; 12-06-2010 at 01:53 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't put bits of code in quotes just because it's fun. I don't see anything wrong with your cases.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    You don't put bits of code in quotes just because it's fun. What exactly do u mean by that?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by Makdaddy
    What exactly do u mean by that?
    He means this:
    Code:
    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 */
    }
    Bits in red do not belong there.



    Also, this...
    Code:
    fflush(stdin);
    Is undefined behavior. fflush should only be used on output streams/buffers, not input.

    And only include the actual code within code tags, not additional non-code bits Daddy Mac (I tried making a Kris-Kross reference there see?).
    "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
    Dec 2010
    Posts
    5
    yah i figured out that the quotes don't need to be there.. Yup im stupid because ive never done this stuff before and asking for help thats what this site is.

    Thanks

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    I figured it out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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