Thread: Restricting Character lengths

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Melbourne, Australia
    Posts
    6

    Restricting Character lengths

    I am having trouble restricting the character lengths in this data entry form I am making using do/while with nested if statements. I have tried to alter these with strlen functions but i can't get it to constain the character length of the array. Basically, I want the person to be only able to input alpha numeric characters for the name of the dog's species with up to 22 characters only.
    Code:
    int main(void)
    {
    	/*variables*/
    	int errors;
    	int i;
    	char dog_s[22] = {'\0'};
    
    do 
    {
    errors = 0;
    printf("Enter The breed, species of the dog");
    printf(" , No more than twenty two characters\n");
    scanf("%22s", dog_s);
    fflush(stdin);
    		
    if (strlen(dog_s) < 0 || strlen(dog_s) > 22)
    errors=1;
    		
    }
    while (errors > 0);
    for (i = 0 ; i <= (int)strlen(dog_s) ; ++i)
    {
    if (isdigit(dog_s[i]) == 0)
    errors=0;
    }
    			
    	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why don't you use fgets() instead of scanf()?

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    fflush(stdin) is wrong. Read why here and read about the solution here.

    You can't prevent the user from typing certain characters, or have the program skip over them as they're typed. Keyboard input is line buffered meaning your program doesn't technically see the input until the user hits enter. You would need a special library like curses/ncurses for this, which is a lot of trouble. Try reading in a whole line with fgets, then check the length with strlen and make sure every character in the string is alphanumeric by using isalnum() from ctype.h.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Melbourne, Australia
    Posts
    6
    Ok, I shall give it a try, could this be a source to why it isn't functioning to restrict character length?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Technically, yes, but probably not. fflush(stdin) results in undefined behavior. The thing about undefined behavior is, it's undefined. Anything or nothing at all, can happen. Usually fflush(stdin) just doesn't do anything, and the odds of it ruining your length check are really slim. Regardless, ditch that and try using fgets to read the input into a buffer, then check that buffer.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Location
    Melbourne, Australia
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    Technically, yes, but probably not. fflush(stdin) results in undefined behavior. The thing about undefined behavior is, it's undefined. Anything or nothing at all, can happen. Usually fflush(stdin) just doesn't do anything, and the odds of it ruining your length check are really slim. Regardless, ditch that and try using fgets to read the input into a buffer, then check that buffer.
    code wise it would look like????

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read the two links in my first post (click the red text). They explain the problem and solution, including examples of using fgets.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The thing is if you use ... scanf("%22s",String);... the user can type as much as they want and the char array only sees one of...
    a) the whole string if less than 22 characters
    b) the first 22 characters if the user types more
    c) the first word if there's more than one word (up to 22 characters)

    You are constraining the array to bounds (although it should be ... dog_s[23]... to allow for the trailing null. But you can't constrain the typist

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 05:35 PM
  2. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  3. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  4. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  5. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM