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;
}