1

can you help me improve my code... this is all about student information... i'm having trouble in syntax... In editing menu... i try using strcmp but nothing happens, i first use fgets and store it at an array and then ask the user for an input and store it again in another array.. and then i'll compare... but it didn't work.. hope you can help me... this is my code..

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student{
       char name[30];
       char id[8];
       char course[5];
};

int main(void){
    int option =0;

    while(option!=6){
       system("cls");
       printf("Menu:\n");
       printf("[1] Add Student.\n");
       printf("[2] Display Student.\n");
       printf("[3] Delete Student.\n");
       printf("[4] Delete Student.\n");
       printf("[5] Exit.\n");
       scanf("%d",&option);

       switch(option)
       {
          case 1:
                addStudent();
                break;
          case 2:
                displayinfo();
                break;
          case 3:
                break;  
          case 4:
                break;
          default:
                printf("That is not in the options!\nPlease Try again!\n");
                break;
       }

    }
}

addStudent(){
    int i;
    FILE *stream = NULL;
    stream = fopen("studentinfo.txt", "a+");

    struct student s1;
    struct student array[3];//here i wnt 2 apply d malloc but, still didn't know how 2start
    for (i =0; i<1; i++){

       printf("Enter Student ID: ");
       scanf("%s", s1.id);
       fflush(stdin);
       printf("Enter Student Name: ");
       gets(s1.name);
       fflush(stdin);
       printf("Enter Student Course: ");
       scanf("%s", s1.course);

       fprintf(stream, "\n%s,\t%s,\t%s", s1.id, s1.name, s1.course);
    }
       fclose(stream);
    getch();
}
displayinfo(){
    FILE *stream = NULL;
     stream = fopen("studentinfo.txt", "rt");

     char arr[100];
     int i=0;

    while(!feof(stream)){  
     fgets(arr, 100, stream);
     printf("%s", arr);
     }

     fclose(stream);
     getch();
}

here's my plan in EDITING MENU:

     printf("enter details: ");
     gets(arr2);

     while(!feof(stream)){ 
        fgets(arr, 100, stream);
        if(strcmp(arr, arr2)==0){
           //code here
        }

     }

will this work?

thanks guys hope you can help me ^_^

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
iamanapprentice
  • 411
  • 5
  • 19
  • 37

2 Answers2

3

fgets() keeps the newline. gets() does not. Hence the strings will never match.

Try reading the manual for a function if you're not COMPLETELY sure what it is doing.

Instead of gets(arr2) try doing fgets(arr2, 100, stdin).

AlastairG
  • 4,119
  • 5
  • 26
  • 41
  • You're welcome. Fancy accepting the answer by clicking on the empty arrow and maybe clicking the up arrow above the 0 to indicate you think this is a good answer? :) – AlastairG Dec 03 '10 at 12:38
  • Avoid `gets` in all circumstances, it's the devil's tool for creating buffer overflows. – Fred Foo Dec 03 '10 at 13:08
1
while(!feof(stream)){ 
    fgets(arr, 100, stream);

use

while(fgets(arr, 100, stream) != NULL) {
    ...
}

if (ferror(stream))
    printf("error in file" "\n");

feof() won't see an error while reading, so it may hang the loop