Thread: while loop char input

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    while loop char input

    Hey guys is there anything wrong with how im getting the input for a single character because for some reason the if statement is not working properly.

    Even if i do enter the correct input it still prints as invalid?

    Code:
    char guessType()
    {
      char type;
      int valid;
      
      printf("Enter guess type((f)lag, (u)ncover or(s)weep:\n");
      
      valid = 0;
      
      while((type = fgetc(stdin) != '\n' && type != EOF))
      {
            
           if(type != 's' || type !='u' || type != 'f')
           {
               printf("invalid");
    	  
           }
           break;
      }

  2. #2
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    Your if() statement should test as follows:
    Code:
    if(type != 's' && type !='u' && type != 'f')
    Last edited by wintellect; 04-09-2006 at 01:33 PM.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >char type;
    >type = fgetc(stdin)
    fgetc() doesn't return a char, it returns an int.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    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. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Can't get loop to work or validation
    By eminem3150 in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2008, 06:21 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM