0
#include <stdio.h>
#include<stdlib.h>
#include<time.h>

struct quizzes{
    int GT, RT, A, C, ADS, HA;
    // group_theory, ring_theory, analysis, C_programming, advanced_data_structures, homological_algebra
};

struct student{
    char name[20],gender, ID[20];
    struct quizzes quiz_1, quiz_2, mid_semester, final, total_score;
    struct student * next;
};

void random_mark(struct quizzes* ptr){
    ptr->GT = 35+(rand()%66);
     ptr->RT = 35+(rand()%66);
      ptr->A = 35+(rand()%66);
       ptr->C = 35+(rand()%66);
        ptr->ADS = 35+(rand()%66);
         ptr->HA = 35+(rand()%66);
    
}

struct student * create_link( int n){
    srand(time(NULL));
    struct student *head_ptr, *current_ptr;
    head_ptr= malloc(sizeof(struct student)); // creating head pointer
    scanf("\n");
     printf("Enter the student name: \n");
     fflush(stdout);
     fgets((head_ptr->name), sizeof(head_ptr->name), stdin);
     printf("Enter student Gender:\n ");
     fflush(stdout);
     head_ptr->gender = getchar();
     scanf("\n");
     printf("Enter student ID:\n ");
     fflush(stdout);
     fgets((head_ptr->ID), sizeof(head_ptr->ID), stdin);
     random_mark(&(head_ptr->quiz_1));
     random_mark(&(head_ptr->quiz_2));
     random_mark(&(head_ptr->mid_semester));
     random_mark(&(head_ptr->final));
//////////////////////////total_score///////////////////////////////////////////
     (head_ptr->total_score).GT =  (head_ptr->quiz_1).GT+ (head_ptr->quiz_2).GT+(head_ptr->mid_semester).GT+ (head_ptr->final).GT;
     (head_ptr->total_score).RT =  (head_ptr->quiz_1).RT+ (head_ptr->quiz_2).RT+(head_ptr->mid_semester).RT+ (head_ptr->final).RT;
     (head_ptr->total_score).A =  (head_ptr->quiz_1).A+ (head_ptr->quiz_2).A+(head_ptr->mid_semester).A+ (head_ptr->final).A;
     (head_ptr->total_score).C =  (head_ptr->quiz_1).C+ (head_ptr->quiz_2).C+(head_ptr->mid_semester).C+ (head_ptr->final).C;
     (head_ptr->total_score).ADS =  (head_ptr->quiz_1).ADS+ (head_ptr->quiz_2).ADS+(head_ptr->mid_semester).ADS+ (head_ptr->final).ADS;
     (head_ptr->total_score).HA =  (head_ptr->quiz_1).HA+ (head_ptr->quiz_2).HA+(head_ptr->mid_semester).HA+ (head_ptr->final).HA;
////////////////////////////////////////////////////////////////////////////////    
     
     
    current_ptr= head_ptr;
    
    
    
    for(int i=1; i<n; i++){        // using current pointer to create link_list;
        (current_ptr->next) =  malloc(sizeof(struct student));
        printf("Enter the student name: ");
        fflush(stdout);
        fgets(current_ptr->next->name, sizeof(current_ptr->next->name), stdin);
        printf("Enter student Gender: ");
        fflush(stdout);
     current_ptr->next->gender = getchar();
     printf("Enter student ID: ");
     fflush(stdout);
     fgets(current_ptr->next->ID, sizeof(current_ptr->next->ID), stdin);
     random_mark(&(current_ptr->next->quiz_1));
     random_mark(&(current_ptr->next->quiz_2));
     random_mark(&(current_ptr->next->mid_semester));
     random_mark(&(current_ptr->next->final));
//////////////////////////total_score///////////////////////////////////////////
     (current_ptr->next->total_score).GT =  (current_ptr->next->quiz_1).GT+ (current_ptr->next->quiz_2).GT+(current_ptr->next->mid_semester).GT+ (current_ptr->next->final).GT;
     (current_ptr->next->total_score).RT =  (current_ptr->next->quiz_1).RT+ (current_ptr->next->quiz_2).RT+(current_ptr->next->mid_semester).RT+ (current_ptr->next->final).RT;
     (current_ptr->next->total_score).A =  (current_ptr->next->quiz_1).A+ (current_ptr->next->quiz_2).A+(current_ptr->next->mid_semester).A+ (current_ptr->next->final).A;
     (current_ptr->next->total_score).C =  (current_ptr->next->quiz_1).C+ (current_ptr->next->quiz_2).C+(current_ptr->next->mid_semester).C+ (current_ptr->next->final).C;
     (current_ptr->next->total_score).ADS =  (current_ptr->next->quiz_1).ADS+ (current_ptr->next->quiz_2).ADS+(current_ptr->next->mid_semester).ADS+ (current_ptr->next->final).ADS;
     (current_ptr->next->total_score).HA =  (current_ptr->next->quiz_1).HA+ (current_ptr->next->quiz_2).HA+(current_ptr->next->mid_semester).HA+ (current_ptr->next->final).HA;
////////////////////////////////////////////////////////////////////////////////
        current_ptr= current_ptr->next;
    }
    current_ptr = NULL;
    return head_ptr;             // returning head pointer;
}

void print_quiz(FILE* fptr, struct quizzes quiz)
{       
        fprintf(fptr, "Group theory: %d\n",(quiz).GT);
        fprintf(fptr, "Ring theory: %d\n",(quiz).RT);
        fprintf(fptr, "analysis: %d\n",(quiz).A);
        fprintf(fptr, "C_programming: %d\n",(quiz).C);
        fprintf(fptr, "Hdvanced_data_structures: %d\n",(quiz).ADS);
        fprintf(fptr, "Homological_algebra: %d\n",(quiz).HA);
}

void print_list( struct student* ptr, int n )
{
    FILE*fptr= fopen("students.txt", "a");
    for(int i=0; i< n; i++){
        fprintf(fptr,"Name: %s", ptr->name);
        fprintf(fptr,"Gender: %c\n", ptr->gender);
        fprintf(fptr,"ID: %s", ptr->ID);
        fprintf(fptr, "QUIZ 1\n");
        /*fprintf(fptr, "Group theory: %d\n",(ptr->quiz_1).GT);
        fprintf(fptr, "Ring theory: %d\n",(ptr->quiz_1).RT);
        fprintf(fptr, "analysis: %d\n",(ptr->quiz_1).A);
        fprintf(fptr, "C_programming: %d\n",(ptr->quiz_1).C);
        fprintf(fptr, "Hdvanced_data_structures: %d\n",(ptr->quiz_1).ADS);
        fprintf(fptr, "Homological_algebra: %d\n",(ptr->quiz_1).HA);*/
        print_quiz(fptr, ptr->quiz_1);
        fprintf(fptr, "QUIZ 2\n");
        print_quiz(fptr, ptr->quiz_2);
        fprintf(fptr, "MID_Semester\n");
        print_quiz(fptr, ptr->mid_semester);
        fprintf(fptr, "Final\n");
        print_quiz(fptr, ptr->final);
        fprintf(fptr, "total\n");
        print_quiz(fptr, ptr->total_score);
        
        
        ptr=ptr->next;
    }
    fclose(fptr);
}
int main(void)
{
    struct student* sptr, *ascending_ptr, *descending_ptr;
    int n ;
    setbuf(stdout, NULL);
    printf("Enter a number: \n");
    fflush(stdout);
   scanf("%d\n", &n);
    sptr= create_link(n);
    print_list(sptr,n);
      
}

The code is quite long, but It's needed for the execution of the program so i added it. But the problem is in the function struct student * create_link( int n), I have used printf and fgets, the fgets start to execute before the printf prints. I have used fflush(stdout) but still, nothing happens. why fflush is not solving this problem? (I also tried fprintf(strerr, "..."); but that didn't help either)

Cloud JR K
  • 181
  • 1
  • 1
  • 7
  • Try removing the `\n` from your `scanf("%d\n", &n);` call in `main`. What do you think that is actually doing? I see you have a few other calls with that newline in the format specifier... – Adrian Mole Apr 17 '21 at 15:09
  • If you're trying to use that newline format to somehow 'flush' the input, then it won't really work: [Behaviour of scanf when newline is in the format string](https://stackoverflow.com/q/21859277/10871073). – Adrian Mole Apr 17 '21 at 15:13
  • i don't think it's because of the newline in scanf. i removed it. it still does the same. – Cloud JR K Apr 17 '21 at 18:02

0 Answers0