Troubleshooting Input Function
Hey Folks,
Anyone know why I'm getting an "incompatible types in return" error for :
Code:
if (fgets_ptr == NULL) return -1;
in Dev C++ 4.9.9.2?
Even when I remove the line, the function still does not behave as intended. I'm trying to take an entry, clear the buffer, and keep taking entries until the user gets it right (string neither to long nor empty), and finally remove the newline if there is one. Can anyone see what's going on here? Thanks.
-SH
Code:
contact input () /* gets an entry from the user */
{
contact entry = {'\0','\0','\0','\0','\0','\0','\0','\0'}; /* initializes the structure */
char *fgets_ptr;
int length;
do{
printf ("\nEnter a new name.\n\n>");
fgets(entry.name, sizeof(entry.name), stdin);
fgets_ptr = fgets(entry.name , sizeof(entry.name), stdin);
if (fgets_ptr == NULL) return -1;
while((junk = getchar()) != '\n' && junk != EOF);
if ( strchr(entry.name, '\n') == NULL ){ /* if overflow */
printf ("\nYour entry is too long, please try again.");
entry.name[0] = '\0';
}
} while (entry.name[0] == '\0' || entry.name[0] == '\n');
length = strlen (entry.name);
if (entry.name[length - 1] == '\n'){
entry.name[length - 1] == '\0';} /* replace \n */
return entry;
}