A .txt file is created containing customer info(it is titled with their FIRSTname_LASTname.txt, depending on what their name is) , but when I select to view the list of customers it is supposed to read the name of the .txt file (their name) and display it, but it isn't. For some reason count is == 0 so it just says "no customers found" even when the .txt file is in the correct place.
struct Info {
char FirstName[20];
char LastName[20];
int AccNumber;
float Balance;
};
int main()
{
int Selection;
printf("\n*******************************\n");
printf("\nWelcome to World Bank!\n");
printf("\nPlease Enter a Selection Below!\n");
printf("\n1: View Customers\n");
printf("2: View Total Assets\n");
printf("3: Administrator View\n");
printf("4: Exit\n");
printf("\n*******************************\n");
scanf("%d", &Selection);
while (getchar() != '\n');
if(Selection == 1){
system("cls");
printf("\nCustomer List\n");
printf("*******************************\n");
char filename[40];
struct Info customer;
int count = 0;
for (char c = 'A'; c <= 'Z'; c++){
for (char d = 'A'; d <= 'Z'; d++){
sprintf(filename, "%c%c.txt", c, d);
FILE *Fpointer = fopen(filename, "r");
if(Fpointer != NULL){
fscanf(Fpointer, "Name: %s %s\nAccount number: %d\nBalance: %f",
customer.FirstName, customer.LastName, &customer.AccNumber, &customer.Balance);
printf("%s %s\n", customer.FirstName, customer.LastName);
fclose(Fpointer);
count++;
}
}
}
if (count == 0){
printf("No Customers Found.\n");
}
getchar();
} if (Selection == 2){
} if (Selection == 3){
int Selection2;
system("cls");
FILE * Fpointer;
printf("Administrator View\n");
printf("\n*******************************\n");
printf("\nPlease Make a Selection:\n");
printf("1: Add New Customer\n");
printf("2: Edit Current Customer\n");
printf("3: Remove Customer\n");
scanf("%d", &Selection2);
while (getchar() != '\n');
if (Selection2 == 1){
char filename[40];
struct Info new_customer;
printf("Enter Customer First Name: ");
scanf("%s", new_customer.FirstName);
while (getchar() != '\n');
printf("Enter Customer Last Name: ");
scanf("%s", new_customer.LastName);
while (getchar() != '\n');
printf("Enter Account Number: ");
scanf("%d", &new_customer.AccNumber);
while (getchar() != '\n');
printf("Enter Balance (No Commas): $");
scanf("%f", &new_customer.Balance);
while (getchar() != '\n');
sprintf(filename, "%s_%s.txt", new_customer.FirstName, new_customer.LastName);
Fpointer = fopen(filename , "w");
if (Fpointer == NULL){
printf("Error: Could not create file.\n");
return 1;
}
fprintf(Fpointer, "Name: %s %s\nAccount number: %d\nBalance: %.2f", new_customer.FirstName, new_customer.LastName, new_customer.AccNumber, new_customer.Balance);
fclose(Fpointer);
}
} else if (Selection == 4){
system("cls");
printf("\n*******************************\n");
printf("Thank you for Banking with us Today!\n");
printf("Press any Key to Exit");
printf("\n*******************************\n");
getchar();
return 0;
}
return 0;
}
I have tried removing the if(count == 0) but it still doesn't work even without that parameter, I have also tried having the .txt file in every possible place it would need to be to read it.