Thread: Quick question

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    Quick question

    Why is my switch statement looping through all scenarios and printing each case.The program works correctly, but it should break after first case if I enter a number.??????.......This is just the part of the code I'm having a problem with.............Thanks.

    Code:
    /* while user does not enter E */
       while ( c != 'E') { 
          switch( c ) { 
    
             /* enqueue value */
             case 'A':
                printf( "Enter a number: " );
                scanf( "\n%d", &item );
                enqueue( &headPtr, &tailPtr, item );
                printQueue( headPtr );
                break;
    
             /* dequeue value */
             case 'R':
    
                /* if queue is not empty */
                if ( !isEmpty( headPtr ) ) { 
                   item = dequeue( &headPtr, &tailPtr );
                   printf( "%d has been dequeued.\n", item );
                } /* end if */
    
                printQueue( headPtr );
                break;
    
               default:
                printf( "Invalid choice.\n\n" );
                instructions();
                break; 
    
          } /* end switch */
    
          printf( "? " );
          scanf( "%c", &c );
       } /* end while */
    
       printf( "End of run.\n" );
       
       return 0; /*

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    It does the case first, but also displays the default senario also, then loops again....Why is it displaying the default part......Maybe something wrong with while statement??????.Thanks for any help.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you have forgotten that \n is a perfectly good character, and therefore every other time through the loop c is enter-key rather than your choice.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    At first glance, you aren't updating C after each switch statement. For example, you enter 'A', and you add a number to the list, then the program breaks and loops back around to the A case again. You need to ask the user to enter C again.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    Good advice.Thanks.....I'll give it another look.........thanks...

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    I don't know....Its just not breaking at the break statement as it should.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    In his post, tabstop has told you everything you need to know to fix the source code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM