help with string length loop.

This is a discussion on help with string length loop. within the C Programming forums, part of the General Programming Boards category; i need some help. i need my code to ask the user for his or her name and check with ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    46

    help with string length loop.

    i need some help. i need my code to ask the user for his or her name and check with it is shorter then 20 characters. here is what ive done

    Code:
      do
      {
       fprintf(output,"please enter your name:\n");fflush(stdout);
       fgets(name, 20, input);
    
    
    
       if (strlen(name)>20)
       {
        fprintf(output,"please enter a name less than 20 characters.\n");
       }
      }
      while (strlen(name)>20);
    now it will input a name fine, but anything equal or greater then 20 doesnt work it will print out 20 characters and then the rest of the characters go into the input buffer.

    but also it doesnt print an error message if its over 20. so what is wrong with my loop.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    fgets() will never allow you to enter a string larger than the buffer you supply.

    The only clue that you have that it was a 'long' name would be the lack of a '\n' in the name to begin with. If this is the case, then the rest of the input remains to be read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    ok 2 problems.

    first how do you stop fgets from including \n within the name.

    and how do you create a loop to repeat until fgets has the right amount of values.

    and if to many values are entered in, how do you remove them from the input buffer.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > first how do you stop fgets from including \n within the name.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    > and how do you create a loop to repeat until fgets has the right amount of values.
    If you mean the right value, then something like:
    Code:
    do {
        fgets(buffer, sizeof(buffer), stdin);
        /* trim the newline character off, if it exists */
    } while(strcmp(buffer, "right value") != 0);

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    well the name can be at most 20 values.

    so i need to check to see if the string entered is less then 20 values. i dont think your check does this.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    if(strlen(name) < 20)
    {
    ?

    But you're only going to see upto 20 characters at most, as Salem said, so in other words, it's redundant.
    What exactly do you want to do?
    A work around could be increase the buffer size in fgets() to say, 32. Then check if they entered a string larger than 32 if they did, it's invalid...

    Code:
    char buf[32];
    char * p;
    
    fgets(buf, sizeof(buf), stdin);
    /* remove the newline if it exists */
    if((p = strchr(buf, '\n')) != NULL)
        *p = '\0';
    
    if(strlen(buf) > 20)
        printf("Invalid name\n");

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    this is what ive done.

    Code:
     
    
     do
      {
       fprintf(output,"please enter your name:\n");fflush(stdout);
       fgets(name, 20, input);
    
      if ((p = strchr(name, '\n')) != NULL)
      {
         *p = '\0';
      }
    
    
       if (strlen(name)>20)
       {
        fprintf(output,"please enter a name less than 20 characters.\n");
       }
      }
      while (strlen(name)>20);
    
      fprintf(output,"your name is: %s\n",name);
    now to test it i just typed in 20 characters. and the return was 19 characters then in went back to the menu and then the menu looped because a key was pressed...

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    at the moment i have to enter my name 3 times before it inputs the name. and it doesnt loop.
    what is wrong.
    please help im having a really hard time

    Code:
     
    
    do 
      {
       fprintf(output,"please enter your name:\n");
       fgets (name, sizeof (name), input);
    
       if (fgets (name, sizeof (name), input)==NULL)
       {   
        fprintf(output,"please enter a name less than 20 characters.\n");
       }
      } 
      while (fgets (name, sizeof (name), input)==NULL);
    
       
      fprintf(output,"your name is: %s\n",name);

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're calling fgets 3 times...

    Explain what exactly you need? Why can't you just ask for 20 characters, as Dave_Sinkula's post shows.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    Code:
    char buff[BUFSIZ];
    if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
      if ( strchr(buff,'\n') == NULL ) {
        // very long line, and there is no newline
        // report the line is too long, and remove excess characters from stdin
        // until a newline is found - see the FAQ
      } else if ( strlen(buff) > 20 ) {
        // the line (+ it's \n) is longer than 20 chars
        // complain
      } else {
        // this line meets your requirements
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM

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