Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Help with strings

    I am new here and hoping to get some help. I wrote a c code to prompt a user to input information and then generate a password using this information. While inputing the middle initial is skipped and I can not figure out why as I am fairly new to strings.
    Thanks for any help!

    Code:
    /* password.c by me, 11/12/12 */
    
    #include <stdio.h>
    #include <string.h>
    
    
    /* Prototype for fixgets */
    int fixgets(char *, int , FILE *);
    
    
    int main ()
    {
        char first [ 10 ], middle [ 1 ], last [ 15 ], stuNo [ 6 ], school [ 20 ], password [ 5 ];
    
    printf("Welcome Student Application");
    
    printf("\n\nFirst Name?                      ");
        fixgets(first, 10, stdin);
    
    printf("%-40s","\n\nMiddle Initial?          ");
        fixgets(middle, 1, stdin);
    
    printf("%-40s","\n\nLast Name?               ");
        fixgets(last, 15, stdin);
    
    printf("%-40s","\n\nStudent Number?          ");
        fixgets(stuNo, 6, stdin);
    
    printf("%-40s","\n\nName of High School?     ");
        fixgets(school, 20, stdin);
    
    /* Processing */
        strcpy(password, last);
        strcat(password, ", ");
        strcat(password, first);
    
        password [ 0 ] = last   [ 0 ];
        password [ 1 ] = middle [ 0 ];
        password [ 2 ] = first  [ 0 ];
        password [ 3 ] = stuNo  [ 0 ];
        password [ 4 ] = '\0';
    
    /* output */
    printf("Your Password is %s\n", password);
        return 0;
    
    }
    
    
    
    
    /* This routine fixes fgets incorporation of line feeds and other
       ASCII values less than 32 into an input string. */
    int fixgets(char input_array[], int length, FILE *file_name)
    {
        register int position=0;
    
        if (fgets(input_array,length,file_name) != NULL)
        {
            while(input_array[position] != 0)
            {              if (input_array[position] < 32) input_array[position]=0;
            else ++position;
            }
        }
    /* Flush the input buffer */
    fflush(stdin);
    
    /* The length of the string is returned. */
    	return position;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    fgets

    char * fgets ( char * str, int num, FILE * stream );

    Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
    For the middle initial, you're passing 1 as the second parameter, so it will read zero characters from the input and just send back a null character.

    You might be tempted to make the second parameter 2, but this alone will not make your program work. Here's why:

    Let's say the middle initial is 'S'. You press 'S' and enter. The input buffer contains "S\n". Only the 'S' is read, and appended with a null character, and the newline floating in the input buffer is picked up by the next call to "fgets()" (skipping the last name prompt).

    ---

    And don't use fflush(stdin)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM