How to resume all students with a common name

Asked

Viewed 120 times

1

I’m developing a program that has to run a school canteen and on the menu I have an option to search student by number and another to search all students who have a first name in common. Here’s my code, where I can’t 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 pretty sure the mistake is small, but I couldn’t fix it. The output I want is if for example there are 20 students with the name Vitor the program shows me the numbers of the 20 students and their names.

  • your database-aluno.txt has that format?

  • As well as file format ??

  • It is an example of content, you use line break or a format type files csv?

  • Line Break, actually 1 student corresponds to a Line

  • Another thing, you have to put the structure of struct Aluno student; why it is not possible to understand how the code works. Friend please read this link: http://answall.com/help/mcve

  • Actually at the beginning of the program I have a structure called student with the variables indicated in fscanf

  • Even calling the structure for example Student[acstruct]. name where acstruct, type int is a "variable to track the string", the program does not give me the intended output

  • Follow the instructions of the link that I gave you please, so you will get more answers probably, it is difficult to get answers when people have to deduce how works a code that only has a piece. I hope you understand. See you

Show 3 more comments

1 answer

3


Your first problem is here:

if (name == student.name)

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

What you wanted is to compare if the strings are equal:

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

There is another problem too, you seem to open the file for reading (although it is 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 function name FindStudentsByName, it doesn’t make much sense you want to write in the file to look for something, don’t you think?

Also, its function of student search does not return anything (and does not fill any memory content of a given pointer). So it’s hard to use it to do any useful work, don’t you think?

Finally, if you want to return all students with the common name, 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.