开发者

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();
}


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 ^_^

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜