Search code examples
csearchsequential

Search returning only the first result in structure C


I am trying to read a file and print the records where the jumper has exceeded the distance provided by the user, but the search only returns the first line in the file and that's it, i know there is something wrong in the logic but i cant put my finger on it.

    #include<stdio.h>
    #include<string.h>

typedef struct {
    char fname[30];
    char lname[30];
    char nationality [30];
    double distance;
}Player;

void print(Player p) {
    printf("%s %s %s %f \n", p.fname, p.lname, p.nationality, p.distance);
}
int search(double distance, Player allPlayers[], int max){
    int i=0;
    for(i=0; i<max; i++){
        if(distance > allPlayers[i].distance){
            return i;
        }
        return -1;
    }
}

void load (char filename[10], Player players[]){

    char tempfName [30];
    char templName [30];
    char tempNationality [30];
    double tmpdistance;
    FILE * input=fopen(filename,"r+");
    if(input==NULL)
        printf("Error found in opening the file\n");
    else {
        printf("The data are loaded successfully.\n");
        int counter = 0;
        while(!feof(input)){

            strcpy(tempfName," ");
            fscanf(input, "%s %s %s %f",tempfName, templName, tempNationality, &tmpdistance);
            if(strcmp(tempfName, " ") !=0){
                strcpy(players[counter].fname, tempfName);
                strcpy(players[counter].lname, templName);
                strcpy(players[counter].nationality, tempNationality);
                players[counter].distance = tmpdistance;
                counter++;
            }
        }
            fclose(input);
    }
}
int main (int argc, char *argv[]){

    Player players2[40];
    char myFileName[10] ="jump.txt";

    load(myFileName, players2);

    double tmpdistance;
    printf("Please enter the distance threshold: "); 
    scanf("%lf", &tmpdistance);
    printf("The jumpers exceeded 8.10 m are: \n"); 
    int index = -1;
    index = search(tmpdistance,players2,40);

    if(index!=-1)
        print(players2[index]);

    else
        printf("NOT Found! \n");

    return 0;
}

some sample data from the file I'm trying to read :

Arsen Sargsyan Armenia 7.62

Boleslav Skhirtladze Georgia 7.26

Christian Reif Germany 7.92

Christopher Tomlinson Great_Britain 8.06


Solution

  • In your search() function:

    if(distance > allPlayers[i].distance){
        return i;
    }
    

    This will return the first jumper whose performance is less than the distance provided.

    If you replace it with:

    if(allPlayers[i].distance > distance){
        return i;
    }
    

    It will return the first jumper whose performance is greater than the distance provided, which is what you want.

    Also:

    1. Don't loop over feof() to read a file: Why is “while ( !feof (file) )” always wrong?
    2. You're printing "The data are loaded successfully" when you haven't loaded the data yet - you've just opened the file
    3. You don't need the arguments to main()
    4. You don't need to initialise index to -1 as you assign it in the next line
    5. You don't need to specify the size of myFileName as the compiler can work it out