Thread: Input Validation Question

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    Input Validation Question

    In my introductory C class I wrote a program that takes two points on the earths surface and calculates the arc distance between them. I finished up the projects, except that I have one question on input validation which is probaly pretty stupid.

    It has to do with one of my input functions that asks the user to enter N or S for latitude and E or W for longitude. The function sends back an integer value to represent the choice of the user. Here is the code for the function:

    Code:
     
    int northsouth()
    {
       char ns_switch;  // Character variable of switch that should have a value of N or S
       int ns_var;  // Integer value of 1 or 2 indicating N or S respectively
       int true_false=0;  // Determines when to quit the while loop
    
       while (true_false == 0)
       {
          printf("Enter N for North or S for South: ");
          fflush(stdin);
          scanf("%c", &ns_switch);
    
          switch (ns_switch)
          {
            case 'N':
            ns_var=1;
            true_false=1;
            break;
     
            case 'S':
            ns_var=2;
            true_false=1;
            break;
    
            default:
            true_false=0;
            printf("\nInvalid Input! Please Try Again! \n");
          }
       }
    return(ns_var);
    }

    If I enter : N
    it works fine
    If I enter : S
    it works fine
    If I enter : (anything that doesn't start with N or S)
    it re-does the loop and works fine.

    But if I type: Newew
    or : Sd323
    or : NS
    The loop will still quit.

    So my question is, How do I fix my program so that it will only accept (N or S) period. If something is typed after N or S, I want the program to see it as invalid input.


    thank you for the help,
    Zack

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Your
    Code:
    scanf("%c", &ns_switch);
    Scans in a character, and I believe that it's taking in only the first letter (The N from "Newew") and setting your ns_switch variable equal to the first character.

    One way around this could be to read the input in as a string instead, and then check to see if the length of the string is 1. If not, it's invalid. If it is, then proceed with the rest of the checks you have in place.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    thanks

    Thanks,
    Im going to look in my C book and see if I can figure out how to do that.

    Zack

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Holy Deja Vu, Batman!

    You should look at the FAQ as to why fflush( stdin ) is wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Because ns_switch is a typ char, your program is only looking at the first character of the input buffer. Since that leaves a possibility of extra data in your input buffer which would require flushing anyway (using fflush(stdin) btw isn't the best method), you could try placing your switch statement inside an if() testing to see if there is anything other than a newline character in the input buffer. If there is, then you know to set true_false to 0.

    Code:
    if ( getchar() == '\n' )
    	switch loop
    else
    	true_false = 0;
    should work.
    Last edited by Scribbler; 10-11-2004 at 11:37 PM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    fflush

    Doesn't that just clear the buffer memory?

    Zack

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zackboll
    Doesn't that just clear the buffer memory?

    Zack
    If you READ THE DAMN FAQ LIKE I SAID you'd know the answer!

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Quzah's mentioned the FAQ twice already, it's explained really well there why fflush(stdin) should be avoided. It's a good read. Sure changed my mind after I read it.

    [EDIT]
    Three times
    [/EDIT]
    Last edited by Epo; 10-11-2004 at 11:42 PM. Reason: Upped the Quzah FAQ count

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    buffer clearing

    Should I worry about clearing the buffer in this program. I read the FAQ. How do you clear keyboard input from buffer?

    thanks,
    Zack

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    int fflush(FILE *ostream);

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I bet if you searched you could find out. -_-;
    Oh, and that o in ostream is for output stream. Which if you read the documentation on... forget it. Just forget it.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    11
    Quote Originally Posted by Scribbler
    Because ns_switch is a typ char, your program is only looking at the first character of the input buffer. Since that leaves a possibility of extra data in your input buffer which would require flushing anyway (using fflush(stdin) btw isn't the best method), you could try placing your switch statement inside an if() testing to see if there is anything other than a newline character in the input buffer. If there is, then you know to set true_false to 0.

    Code:
    if ( getchar() == '\n' )
    	switch loop
    else
    	true_false = 0;
    should work.

    thank you Scribbler, what you suggested worked really well. I just need to figure out how to clear the buffer before inputs now.

    Zack

  13. #13
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Wow...a slew of replies without a question being answered when only one reply whould have done it...

    while ( getchar() != '\n' );

    will flush it for you. The while loop will cycle each char through until it hits the newline. At which point it's done. Just be careful where you put it. If there's nothing in the input buffer, then your program will wait for you to input something from the keyboard. Make sure it's AFTER your scanf.
    Last edited by Scribbler; 10-11-2004 at 11:59 PM.

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    11

    Thanks Again

    thanks again, Im new to C so im not very good

    Zack

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Scribbler
    Wow...a slew of replies without a question being answered when only one reply whould have done it...
    Yeah, a bunch of replies, which had anyone actually paid attention to my sig, would have answered all of your questions for BOTH OF YOU.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input validation loop failure
    By MacNilly in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2006, 03:29 AM
  2. input validation making sure only an int is entered
    By bazzano in forum C Programming
    Replies: 10
    Last Post: 09-06-2005, 06:14 AM
  3. Newbie Question: File Input and Relative Paths
    By Ashes999 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2003, 04:21 AM
  4. Replies: 2
    Last Post: 05-12-2003, 04:40 PM
  5. quick question: File Input
    By meltingdude in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 02:02 AM