How to resume all students with common names

1

I am developing a program that has to manage a canteen of a school and on the menu I have an option to search for student by number and another to search for all students who have the first name in common. Here is my code, where I can not see my problem:

void FindStudentsByName(FILE *f) {  
     //FILE *file;  
     //const char *filename = "database-aluno.txt";     
     //file = fopen(filename,"r"); 
     struct Aluno student;  
     char name[50];     
     printf("Insira Nome (Primeiro nome) ");    
     scanf("%s", name);     
     while (!(feof(f)))     
     {      
        fscanf(f, "%s %s %s %lf %d-%d\n", &student.num, student.name,
 student.fname,&student.saldo, &student.dia, &student.mes);

        if (name == student.name)       
        {           
            fprintf(f,"Numero: %d \n", student.num);
            fprintf(f,"Nome: %s\n ", student.fname);        
        }   
     }
 }

I'm almost sure that the error is small, but I could not resolve it. The output I want is if for example there are 20 students with the name vitor the program show me the numbers of the 20 students and their names.

    
asked by anonymous 25.12.2014 / 00:38

1 answer

3

Your first problem is here:

if (name == student.name)

This will check if the two pointers are pointing to the same memory address, which will never happen.

What you wanted was to compare the strings to the same ones:

if (strcmp(name, student.name) == 0)

There is another problem too, you seem to open the file for reading (although it's commented):

//file = fopen(filename,"r");

But try to write on it (but you opened it for reading, not writing):

fprintf(f,"Numero: %d \n", student.num);
fprintf(f,"Nome: %s\n ", student.fname);

In addition, given the name of the FindStudentsByName function, it does not make much sense to want to write in the file to look for something, do not you think?

Also, your student search function returns nothing (and does not fill any memory contents of any given pointer). So it's hard to use it to do any useful work, do not you think?

Finally, if you want to return all students with the common name, you will need to assemble a list or array of students, right?

Maybe what you wanted is this:

int FindStudentsByName(FILE *f, struct Aluno **array, int max) {  
    char name[50];
    int indice = 0;
    struct Aluno student;
    printf("Insira Nome (Primeiro nome) ");
    scanf("%s", name);
    while (!(feof(f)) && indice < max)
    {
        fscanf(f, "%s %s %s %lf %d-%d\n", &student.num, student.name,
                student.fname, &student.saldo, &student.dia, &student.mes);

        if (strcmp(name, student.name) == 0)
        {
            array[indice]->num = student.num;
            strcpy(array[indice]->name, student.name);
            strcpy(array[indice]->fname, student.fname);
            array[indice]->saldo = student.saldo;
            array[indice]->dia = student.dia;
            array[indice]->mes = student.mes;
            indice++;
        }
    }
    return indice;
}
    
25.12.2014 / 05:34