Hello to all.
I'm a newbie at C programming and this forum as well. A friend told me that I could get some good advice regarding C programming here.
I've written a simple program which ask user to put student name and result. ( the input will be saved into a file called "test.dat")
I'm having a problem with the program I wrote. I was able to compile and run it and it did work. The only problem was it won't display the student name and mark.
here is the code
Code:#include <stdio.h> #include <stdlib.h> struct student { char name[20]; int result; } s1[100]; main() { int menu,stuNum,counter; printf("1.Add\n\n"); printf("2.Display\n\n"); printf("3.Exit\n\n"); printf("Select Menu: "); scanf("%d", &menu); if(menu == 1) { FILE *fp; fp = fopen("test.dat", "w"); system ("cls"); printf("How many student result do you wish to enter? "); scanf("%d", &stuNum); for(counter = 0; counter < stuNum; counter++) { printf("\nEnter student name: "); scanf("%s", s1[counter].name); printf("Enter student result: "); scanf("%d", &s1[counter].result); } fclose(fp); } if (menu == 2) { FILE *fp; char ch; fp = fopen("test.dat","r"); if (fp == NULL) { printf("can't open file"); exit(1); } ch = getc(fp); while(ch!=EOF) { putchar(ch); ch=getc(fp); } fclose(fp); system ("cls"); } if (menu == 3) { system ("cls"); exit(1); } }
I was hoping you guys can help me to solve this problem. I'll post in the code I've written. I hope someone can give me some insights on how I can solve this...
Thanks in advance.