Thread: While loop question

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    While loop question

    This is just a portion of my code and I know everything else works properly, but I need the code to check and see if the user entered a valid input (either 'H', 'S', or 'C'). I want the program to loop until the user inputs a valid variable, but when the user initially puts in one of the variables the program just hangs. It works fine if the user enters an invalid answer (it loops until a valid answer is entered) but then hangs again. Any ideas? I'm thinking the while loop is the issue...how do I get it to work? Thanks.


    Code:
    while ( ( status = getchar() ) != 'H' || 'S' || 'C' )
    {
       switch ( status )
       {
          case 'H':   
          case 'h':  
            break;
    
          case 'S':  
          case 's':  
            break;
    
         case 'C':   
         case 'c':    
            break;
    
         case '\n':  
         case '\t':   
         case ' ':   
            break;
    
         default: /* Catches all characters that are not valid entries */
            printf("Incorrect status value entered.\n");
            printf("Enter a valid status value [H for hourly, S for salaried staff, or C for contractor]:\n");
       }
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What's wrong with this code?
    Code:
    int n = 5;
    
    //do something if n is 5 or 3 or 1
    if (n == 5 || 3 || 1)
    {
    ...
    }
    gg

    [EDIT]
    Solution:
    Code:
    //do something if n is 5 or 3 or 1
    if ((n == 5) || (n == 3) || (n == 1))
    {
    ...
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    I tried doing that as well, but it still doesn't work, unless I am missing something obvious...Thanks.

    Code:
    while ( ( status = getchar() != 'H') || ( status = getchar() != 'S') || ( status = getchar() != 'C') )
    {
    ...
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's one way
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int c;
      int Invalid = 1;
    
      while (Invalid && (c = getchar()) != EOF)  
      {
        Invalid = 0;
        switch (c)
        {
        case 'H': puts("H entered"); break;
        case 'I': puts("I entered"); break;
        case 'J': puts("J entered"); break;
        default:  
          puts("Invalid! "); 
          Invalid = 1; 
          while ((c = getchar()) != '\n' && c != EOF); /* Flush input buffer */
          break;
        }
      }
    
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. while loop question
    By rayrayj52 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 05:13 PM