validating input using scanf

This is a discussion on validating input using scanf within the C Programming forums, part of the General Programming Boards category; The following piece of code checks the users password, if it's not equal to the users password then it just ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    validating input using scanf

    The following piece of code checks the users password, if it's not equal to the users password then it just asks them to enter their password again. The only problem is when i enter a character i.e. # or A or , the while loop i just runs continuously. I'm guessing it's because i've asked scanf to read in digits not chars, I'm not sure as to how i'm suppose to check both chars and ints using scanf at once?

    Code:
       int password;
       printf("Please enter your password:");
       scanf("%d", &password);
    
    
       while (password != 233)
       {
          printf("%s", "Wrong password, please try again:");
          scanf("%d", &password);
       }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your best bet is to use fgets to retrieve input and then sscanf to parse the input. scanf is a problematic function.

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by anonytmouse
    Your best bet is to use fgets to retrieve input and then sscanf to parse the input. scanf is a problematic function.
    Hmm it doesn't seem to work:


    Code:
      char  password[2];
      char password2;
    
      fgets(password, 2, stdin);
      scanf(password, &password2);
      printf("%s", "Password is:");
      printf("%c", password2);
    it just prints out nothing.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    anonytmouse didn't make a typo. He really did mean sscanf() as opposed to scanf(). You can see the differences here. scanf() reads from a stream but sscanf() reads from a string.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Ok i tried using sscanf if i put sscanf it doesn't seem to ask for any input which is strange?

    Code:
      fgets(password, 2, stdin);
      sscanf(password, &password2);
      printf("%s", "Password is:");
      printf("%c", password2);

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    I think you want something like this:
    Code:
    #include <stdio.h>
    int main()
    {
        char p_str[256];
        int p_num;
        do {
            printf("Enter your password: ");
            fflush(stdout);
            fgets(p_str, sizeof p_str, stdin);
        } while (!sscanf(p_str, "%d", &p_num));
        printf("Password is: %d\n", p_num);
        return 0;
    }
    The while loop is necessary because sscanf will return 0 if it doesn't find a number, so you need to ask again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 09:31 PM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 04:54 PM
  4. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 05:34 AM
  5. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 09:21 PM

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