Thread: Question about user input validaton

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    44

    Question about user input validaton

    So I need to validate the input that the user enters. The user has to enter his name , and the program must check that the name's characters are from the alphabet , but the single quote ' and space can also be used.

    No other symbols or punctuation must be used.

    I know how to do all these validations , except the one where the single quote character has to be valid ad not assumed as other punctuation. I did the part where it checks that it name is from the alphabet , but how can I escape the single quote character and the space , so that they are also valid ?

    I did this to validate if they are from alphabet , how can I modify this so that it can also accept single quotes and spaces ?

    Code:
    for (i = 0; i < length; i++)
            {
                    if (!isalpha(name[i]))
                    {        
                        printf("No punctuation is allowed except single quotes and spaces.\n\n");
                            return 0;
                    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You could probably just get rid of isalpha and use ASCII code verification to ensure that the characters in the input are in the 'a-z','A-Z' or ' ranges. I think isalpha does the same thing for letters anyway.

    Check here for the full table: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    but how can it also accept single quote characters and spaces , but does not accept other punctuation marks ?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You have to write your own condition and get rid of isalpha:

    i.e. if((mychar > 'a' and mychar < 'z')
    OR
    (mychar > 'A' and mychar < 'Z')
    OR
    mychar = '\'')
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Or you can even use isalpha and add the extra condition. If isalpha(char) OR char = '\''
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    I am using this conditon :

    name[i] !='\'' && name[i] !=' ' && !isalpha(name[i])

    and it is working. The only problem I am seeing is that when for example the user enters : "Michael Paul" <-- testing the space condition , the second part "Paul" , is filling the surname[] field, instead of continuing on in the name field.

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's probably because you are reading strings with scanf and scanf stops at spaces.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    what should I use then please?

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Google fgets() and/or search this forum for examples. There are at least a dozen or so every single day. It is a common mistake for beginners to use scanf which stands for scan format, to read in nonformatted data, such as names.

    EDIT: scanf is also bad for reading strings because you cannot control how much input you are willing to accept, potentially overflowing your storage.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    44
    oh ok thanks.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by claudiu View Post
    EDIT: scanf is also bad for reading strings because you cannot control how much input you are willing to accept, potentially overflowing your storage.
    Code:
        scanf("%20s", buf);
    willl read only 20 chars, you need a buffer of 21 chars including the \0
    Kurt

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Incidentally hyphenated names are quite common. It is fairly common in some circles that children get a combination of surnames. Have a look here for examples.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question On Putting User Input into Arrays
    By NinjaFish in forum C Programming
    Replies: 4
    Last Post: 04-06-2011, 09:56 AM
  2. User determined input question
    By lyoncourt in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 06:10 PM
  3. newbie question regarding user input
    By cantore in forum C Programming
    Replies: 4
    Last Post: 03-05-2006, 08:57 PM
  4. Testing user input question?
    By Hoser83 in forum C Programming
    Replies: 18
    Last Post: 02-15-2006, 01:18 PM
  5. cin.getline question (user's input too long)
    By JerryL_MB in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2002, 12:17 PM