0

I have a looped struct array that accepts student name and roll number for as many students the user inputs

Code:

int x;
struct studs
{
    char name[50];
    char rollno[50];
}student[50];

            printf("\t\t________________________________");
            printf("\n\n\t\t\tStudent Registration");
            printf("\n\t\t_______________________________");
            printf("\n\n\tHow many students in %s ", course);
            scanf("%d", &stud);

            admin = fopen(name, "a");
            fprintf(admin, "\nNumber of students for %s: %d", course, stud);
            fclose(admin);

            for(x=0; x<stud; x++)
            {
                printf("\n\nStudent %d", x+1);
                printf("\n_____________________________");
                printf("\n\nStudent Name : ");
                scanf("%s", student[x].name);
                printf("\nStudent Rollno. : ");
                scanf("%s", student[x].rollno);
                system("\npause");
                goto exam;
            }
            admin = fopen("student.txt", "w");
            fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);
            fclose(admin);

As you can see i'm trying to put each name and number into the text file but I keep getting this errors

error: request for member 'name' in something not a structure or union | error: request for member 'rollno' in something not a structure or union

Is there anyway to get this into a file, with or without the struct?

  • This isn't the full code sorry, the name in the first fopen is for the administrator of the program. Irrelevant to the question though but just in case anyone was wondering – Elizabeth Ekefre Nov 25 '15 at 22:02
  • 1
    The error you are getting is caused by the fact that `student[50]` is an array of structs. So doing this `student.name` cant work, you need to define which student in the list you want ie. `student[0].name`. – Fantastic Mr Fox Nov 25 '15 at 22:04
  • 2
    `goto exam;` is that a joke? More seriously, the way you attempt to use `fprintf` to write all the records to a text file is quite wrong. – Weather Vane Nov 25 '15 at 22:05

1 Answers1

1

This

fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student.name, student.rollno);

cannot work, student is an array of stud's (or at least it would be if i were in the school). You have already shown that you know how to iterate over the number of students with for(x=0; x<stud; x++), so just do it again:

for(x=0; x<stud; x++) {
    fprintf(admin, "Name: %s \nRoll Number: %s\n\n", student[x].name, student[x].rollno);
}

There are likely going to be other errors in your program (Like that goto exam immediately jumping out of your for loop to somewhere). But this will at least deal with the error you have described.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Thank you so much that solved the problem :] Would you by any chance know hot to read a particular string from the text file? Like i input up to fifty student names how would I pick one name to check if it exists or not? – Elizabeth Ekefre Nov 27 '15 at 06:26
  • @ElizabethEkefre This is a different question that has been answered many times. Do some research, have a go, and if you run into problems ask another question here at SO. – Fantastic Mr Fox Dec 01 '15 at 17:41