I'm trying to add a username, password, firstname and lastname on a text file. Everytime I run the program, it only accepts one username, password, etc. Can anyone please help? here's my code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char UName[50];
char PWord[50];
char FName[50];
char LName[50];
} list;
char search(list *ptr, int *count, char search_val[50])
{
int i, found = -1;
for (i = 0; i < *count; i++)
{
if (((ptr + i)->UName) == search_val)
{
found = i;
break;
}
}
return found;
}
void SignUp (list *ptr, int *count)
{
if (*count == 100)
printf("\nSorry, the maximum users has been reached. ");
else
{
printf("\nEnter a Username: ");
fscanf(stdin, "%s", (ptr + *count)->UName);
if (search(ptr, count, (ptr + *count)->UName) == -1)
{
printf("Enter a Password: ");
fscanf(stdin, "%s", (ptr + *count)->PWord);
printf("Enter your First Name: ");
fgets((ptr + *count)->FName, 50, stdin);
fgets((ptr + *count)->FName, 50, stdin);
printf("Enter your Last Name: ");
fgets((ptr + *count)->LName, 50, stdin);
*count = *count + 1;
}
else
printf("\nUsername %s already exists.\n", (ptr + *count)->UName);
}
}
int main()
{
friendster rec[101], *rec_ptr;
FILE *file_ptr;
char enter;
int done = 0;
int ctr = 0;
int *count;
file_ptr = fopen("Users.txt", "r+tb");
rec_ptr = rec;
while (!done)
{
printf("\n[S]ign up");
printf("\n[L]og in");
printf("\n[E]xit\n");
printf("\nPlease choose any options above: ");
scanf(" %c", &enter);
switch (enter)
{
case 'S':
case 's':
{
SignUp(rec_ptr, &ctr);
fprintf(file_ptr, "%s\n%s\n%s%s*\n", rec_ptr->UName, rec_ptr->PWord, rec_ptr->FName, rec_ptr->LName);
break;
}
case 'L':
case 'l':
{
}
case 'E':
case 'e':
{
printf("Goodbye!!!");
rewind(file_ptr);
done = 1;
break;
}
default:
{
printf("\nThat is invalid.\n");
break;
}
}
}
fclose(file_ptr);
}