Angus Ryan
Angus Ryan

Reputation: 151

Double Printing - C

I am reading from a file, however if it doesn't exist is meant to print 'Read error' but for some reason it is printing read error twice and I don't know why

int loadFlights(char flightDatabase[50], int totalflights,  flight_t f[MAX_NUM_FLIGHTS]) 
{
    int counter;

    FILE *fp; 
    fp = fopen("database.txt", "r");

    if(fp == NULL) { /************************statement with problem*/
        printf("Read error\n");
        return 1;
    }   
    fscanf(fp, "%d",  &totalflights);
    if (totalflights > 5) {
        totalflights = 5;
    }
    for(counter = 0; counter <= totalflights-1; counter++) 
    {
        fscanf(fp, "%s %d %d %d %d %s %d %d %d %d", f[counter].flightcode, 
        &f[counter].departure_dt.month, &f[counter].departure_dt.date, 
        &f[counter].departure_dt.hour, &f[counter].departure_dt.minute, 
        f[counter].arrival_citycode, &f[counter].arrival_dt.month, 
        &f[counter].arrival_dt.date, &f[counter].arrival_dt.hour, 
        &f[counter].arrival_dt.minute);
    }
    fclose(fp);
    return totalflights;
}

I've tried putting an if statement around the Read Error if statement saying if its already been printed don't print again however it still seems to be printing.

int main(void)
{
    flight_t f[MAX_NUM_FLIGHTS];
    int totalflights = 0, menu;
    char flightDatabase[50] = "database.txt";
    while (menu != 5)
    {
        print_Menu();
        scanf("%d", &menu);
        while ((menu < 0) || (menu > 5)) {
            printf("Invalid choice\n");
            print_Menu();
            scanf("%d", &menu);
        }
        if (menu == 1) 
        {
            addFlight(f, totalflights);
            totalflights++; 
        }
        else if (menu == 2) 
        {
            displayFlight(f, totalflights); 
        }
        else if (menu == 3) 
        {
            saveFlight(f, flightDatabase,  totalflights);
        }
        else if (menu == 4) 
        {
            loadFlights(flightDatabase, totalflights, f);
            totalflights = loadFlights(flightDatabase, totalflights,f);
        }
    }
    return 0;
}

This is the code where I call on the function.

Upvotes: 0

Views: 70

Answers (1)

WedaPashi
WedaPashi

Reputation: 3872

This is where the problem is:

// Some code

else if (menu == 4) 
{
    loadFlights(flightDatabase, totalflights, f);
    totalflights = loadFlights(flightDatabase, totalflights,f);
}

These are two consecutive calls to loadFlights while the first call doesn't catch the return value. You can get rid of the first one and it should behave the way you expect it to.

Additionally, I see a problem:

while (menu != 5)

At this point, menu is uninitialised, will hold a random value. You might want to either initialise it to zero or 5 or whatever is legal for that data type.

I've tried putting an if statement around the Read Error...

These are the patch works, that are really dangerous to have. Its usually expected to debug the code and find out whats the exact problem rather than adding a patch to cover up an existing bug.

Upvotes: 1

Related Questions