0

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.

TJP
  • 11
  • 2

1 Answers1

0

The path you write to:

sprintf(filename, "%s_%s.txt", new_customer.FirstName, new_customer.LastName);

is different than the path you read:

                sprintf(filename, "%c%c.txt", c, d);

Always check return values otherwise you are wasting your time. This defects tells you that you want to eliminate code duplication. Unless you have a requirement that says otherwise I suggest you use a single file, say, customers.txt. Alternatively, you either need to look at the current directory to find suitable files, or maintain an index of existing customer files at a well known name. Lookup the customer in the index, then open it file it tells you.

system("cls") is windows terminal specific; consider using an ANSI sequence instead.

Use an enum and/or constants for your menu choices so you can do:

    if(Selection == CUSTOMER){

instead of:

    if(Selection == 1){

This will trigger an infinite loop if you enter EOF (ctrl-D on Linux):

while (getchar() != '\n');

You want to do:

for(;;) {
   int ch = getchar();
   if(ch == EOF || ch == '\n') break;
}

I suggest you create a function for this, or even better, switch to reading a line of input with fgets() then use sscanf() to extract what you need from it. Ignore unexpected input.

Allan Wind
  • 23,068
  • 5
  • 28
  • 38