Search code examples
strcmp

compare fgets to the user input


can you help me with my code? I want to do a program that will determine if the student id was already used, i can compare them once... but what i want to do is to have a comparison every time the user inputs another student id so... the program will know if the user inputs another used id, i know i need to have a loop before the "Enter student id:".. but still having a hard time to think for the conditions, or if you have a better solutions... i would be happy using it.. guys this is my code:

#include<stdio.h>
#include<stdlib.h>
struct studentinfo{
       char id[8];
       char name[30];
       char course[5];
}s1;
main(){
    int i=0;
    int count=0;
    char arr[50];
    FILE *stream = NULL;
    stream = fopen("studentinfo.txt", "a+");    
    struct studentinfo *array[50];

    array[i] = (struct studentinfo*) malloc(sizeof(struct studentinfo));

          printf("Enter Student ID: ");
          scanf("%s", array[i]->id);
          fflush(stdin);
          while(!feof(stream)){ 
            fgets(arr, 6, stream);
             if(strcmp(arr, array[i]->id)==0){
             printf("Student ID is used!\n");
             free(array[i]);
          }
       }
          printf("Enter Student Name: ");
          gets(array[i]->name);
          fflush(stdin);
          printf("Enter Student Course: ");
          scanf("%s", array[i]->course);

          fprintf(stream, "\n%s,\t%s,\t%s", array[i]->id, array[i]->name, array[i]->course);
          i++;

       fclose(stream);
       i=0;//for freeing the space
       if(array[i] != NULL){
       free(array[i]);
       }
    getch();
}

Solution

  • i was advice to use goto function... and it solve the problem but i was kinda worried because there might have a bug i haven't encountered yet, this is my new code:

    #include<stdio.h>
    #include<stdlib.h>
    struct studentinfo{
           char id[8];
           char name[30];
           char course[5];
    }s1;
    main(){
        int i=0;
        int count=0;
        char arr[50];
        FILE *stream = NULL;
        stream = fopen("studentinfo.txt", "a+");    
        struct studentinfo *array[50];
    
        array[i] = (struct studentinfo*) malloc(sizeof(struct studentinfo));
              studid:
              printf("Enter Student ID: ");
              scanf("%s", array[i]->id);
              fflush(stdin);
              while(!feof(stream)){ 
                fgets(arr, 6, stream);
                 if(strcmp(arr, array[i]->id)==0){
                 printf("Student ID is used!\n");
                 goto studid;
                 }
              }
              printf("Enter Student Name: ");
              gets(array[i]->name);
              fflush(stdin);
              printf("Enter Student Course: ");
              scanf("%s", array[i]->course);
    
              fprintf(stream, "\n%s,\t%s,\t%s", array[i]->id, array[i]->name, array[i]->course);
              i++;
    
           fclose(stream);
           if(array[i] != NULL){
           free(array[i]);
           }
        getch();
    }
    

    any other better solutions thnx ^_^