🔒 Closed Dahil wla na sa utak ko help po mabilisan lng

Status
Not open for further replies.
May code ako ganito

For(i=0;i<5;i++)
{
Fscanf(fr,"%s",s[ i]. Id);
Fscanf(fr,"%s",s[ i]. Name);
Fscanf(fr,"%s",s[ i]. Course);
For(j=1;j<=5;j++)
{
Fscanf(fr,"%s",s[ i]. Score[j ]);
Fscanf(fr,"%s",s[ i]. Grade[j ]);
}
}

Gusto ma lagay sila lahat like this
While(fscanf(fr,"%s\n%s\n%s\n",s[ i]. Id,s[ i]. Name,s[ i]. course)!=EOF)

Paano ko ilalagay yung sa score at grade mero bang ibang paraan dyan?

Sana may sumagut
 
Is there a problem with your code? Seems fine to me, unless I'm getting dumber and dumber lol
Im just thinking how can i insert like this
While(fscanf(fr,"%s\n%s\n%s\n",s[ i]. Id,s[ i]. Name,s[ i]. course)!=EOF)
The s[ i]. Score[j ] and s[ i]. Grade[j ] in the while format
 
You are reading from a file fr right?
You can do it, but to prevent error, separated fscanf pa din gamitin mo.
And make sure your while condition is correct too.
Code:
while(!eof){
       fscanf;
       lineCounter++;
}
 
You are reading from a file fr right?
You can do it, but to prevent error, separated fscanf pa din gamitin mo.
And make sure your while condition is correct too.
Code:
while(!eof){
       fscanf;
       lineCounter++;
}

Fscanf; lng buh ang eh lalagay? Then para saab yang linecounter?
 
file: main.c
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct Student Student;

struct Student {
    char id[32];
    char name[32];
    char course[32];
    int score[5];
    float grade[5];
};

Student students[100];
int numStudents=0;

void readfile() {
    FILE *f=fopen("database.txt","rt");
    numStudents=0;
    while(fscanf(f,"%s %s %s %d %d %d %d %d %f %f %f %f %f\n",
            students[numStudents].id, students[numStudents].name, students[numStudents].course,
            &students[numStudents].score[0], &students[numStudents].score[1], &students[numStudents].score[2], &students[numStudents].score[3], &students[numStudents].score[4],
            &students[numStudents].grade[0], &students[numStudents].grade[1], &students[numStudents].grade[2], &students[numStudents].grade[3], &students[numStudents].grade[4])==13) {
            numStudents++;   
    }
}

int main() {

    int i,j;
    
    readfile();
    
    for(i=0;i<numStudents;i++) {
        printf("id: %s name: %s course: %s\n", students[i].id, students[i].name, students[i].course);
        for(j=0;j<5;j++) {
            printf("score[%d]: %d grade[%d]: %6.2f\n",j+1,students[i].score[j],j+1,students[i].grade[j]);
        }
        printf("\n");
    }
    
    return 0;
}

file: database.txt
Code:
000 harry bscs 1 1 1 1 1 75.00 75.00 75.00 75.00 75.00
001 ron bscoe 2 2 2 2 2 85.00 85.00 85.00 85.00 85.00
002 hermione hrm 3 3 3 3 3 95.00 95.00 95.00 95.00 95.00
 
Status
Not open for further replies.

About this Thread

  • 5
    Replies
  • 618
    Views
  • 3
    Participants
Last reply from:
siesta

Online now

Members online
660
Guests online
790
Total visitors
1,450

Forum statistics

Threads
2,278,921
Posts
28,986,795
Members
1,227,642
Latest member
bunene22
Back
Top