Thread: clearing a variable

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    clearing a variable

    Is there a way to reset a char variable?

    I have a getchar() statement that I use over and over to dictate which leg of a switch statement I dive into. I need to clear the variable so that time I go through the while loop I can make a different choice. For debug purposes I used a printf() statement to display it and there is nothing in to display I need to reset the variable and allow the user to re-enter it.

    Here is the code:
    Code:
    \
    while(days_left > 0){
                    
          
          if (days_left == 31){
              my_money = 5000;
               }
                        
                    
          printf(" Day %d of your quest. Where do you want to go\n", days_left);
          printf(" You have %d dollars in your roll\n", my_money);
          printf("----------------------------------------------\n");
          
          printf("A: Chicago\n");   
          printf("B: Detroit\n");  
          printf("C: Disneyland\n");    
          printf("D: Mexico City\n");  
          printf("E: Vatican City\n"); 
          printf("F: Columbo\n");
          
          char city_choice_selection;
    
    
          city_choice_selection = getchar();
    
    
          switch( city_choice_selection)
          {
          case( 'A' ): 
                {   
                    
                    char city[] = "Chicago";
           my_function(&my_money);
                          printf("The ending city choice is %c\n", city_choice_selection);
                          
                          break;
           
           case( 'B' ):
                 break;
                 
                 
           case( 'C' ):
                 break;
                 
           case( 'D' ):
                 break;
    
    
    
    
           default:
                   printf("this is the default\n");
                   printf("The ending city choice is %c\n", city_choice_selection);
           
           }
    days_left = days_left - 1;
    
    
    
    
    
    
    
    
    }
    Thank you

  2. #2
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Would you please explain a bit more!
    Your question is not clear!

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I think you have a fundamental misunderstanding of the way assigments work. During each cycle of the loop a character is read from stdin with getchar and assigned to city_choice_selection. I don't see anywhere in that loop where "resetting" it will have any effect.

    Now, think about what happens when the user types "A<enter>". Your code will read the A character and will select Chicago. Next time through the loop your code will read the <enter> (aka newline or \n) character and will select the default case.

    There are a few different methods to deal with this (and let me emphasize that fflush(stdin) is not a valid method!). Here's one method: FAQ > Flush the input buffer - Cprogramming.com

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    Yes, This is exactly my problem I apologize but there is a skill to asking good questions on this forum and I have yet to master it. I do appreciate your attempts to figure out what I need very much. Thanks!

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    I prefer to use same type of input throughout my programs. For example, I usually don't mix scanf() and fgets(). Also, I will write a function to handle user input and use that function. The resulting code seems cleaner to me.

    Since you are already using fgets(), why not keep using fgets() instead of getchar()? If you provide a big enough char array, in normal use, there shouldn't be any chars left in the input buffer.

    Here is one way:

    Code:
    void menu ()
    {
        char input[8192]; /* buffer is large enough, unless we have malicious user or user error? */
        printf ("In menu(): Enter A, B, or C: ");
        fgets (input, sizeof (input), stdin); /* fgets() will grab up to 8191 chars including the newline char from input buffer and will use last char in array for the '\0' char. */
        switch (input[0]) /* we only care about 1st char in the string. */
        {
            /* catch both upper and lower case */
            case 'A':
            case 'a':
                printf ("Case A, a\n");
                break;
            case 'B':
            case 'b':
                printf ("Case B, b\n");
                break;
            case 'C':
            case 'c':
                printf ("Case C, c\n");
                break;
            default:
                printf ("WTF?\n");
        }
    }
    Note: if you mix getchar() with the above, you may still have to take care of extra chars in the input buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clearing a variable
    By Suchy in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2006, 06:36 AM
  2. Clearing the MRU.
    By The_Inferno in forum Windows Programming
    Replies: 2
    Last Post: 08-19-2005, 05:41 PM
  3. Clearing a variable?
    By Welshy in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2005, 01:30 AM
  4. clearing a variable
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 03:49 PM
  5. Clearing.
    By Krullt in forum C Programming
    Replies: 3
    Last Post: 11-19-2001, 02:04 PM