Ok I finally got enough time to finish this exercise... Here's the full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student Node;
struct student {
int id;
char fname[20];
char lname[20];
float grade;
struct student *next;
};
void saveToFile(Node *list) {
Node *current = list;
FILE *p = fopen("students.txt", "w");
if(p != NULL) {
while(current != NULL) {
fprintf(p, "%d %s %s %3.2f\n", current->id, current->fname, current->lname, current->grade);
current = current->next;
}
fclose(p);
} else {
printf("File cannot be open!\n");
}
}
void findByID(int value, Node *list) {
Node *current = list;
while(current != NULL) {
if(current->id == value)
printf("\n\nFirst Name: %s\nLast Name: %s\nGrade: %3.2f\n", current->fname, current->lname, current->grade);
current = current->next;
}
}
void updateGrade(int value, Node *list) {
Node *current = list;
float newGrade;
while(current != NULL) {
if(current->id == value) {
printf("Enter the student's new grade:\n");
scanf("%f", &newGrade);
current->grade = newGrade;
printf("\n\nStudent ID: %d\nFirst Name: %s\nLast Name: %s\nUpdated Grade: %3.2f\n", current->id, current->fname, current->lname, current->grade);
}
current = current->next;
}
saveToFile(list);
}
void findByName(char fname[], char lname[], Node *list) {
Node *current = list;
while(current != NULL) {
if((strcmp(fname, current->fname) == 0) && strcmp(lname, current->lname) == 0)
printf("\n\nStudent ID: %d\nFirst Name: %s\nLast Name: %s\nGrade: %3.2f\n", current->id, current->fname, current->lname, current->grade);
current = current->next;
}
}
void printAll(Node *list) {
Node *current = list;
while(current != NULL) {
printf("\n\nStudent ID: %d\nFirst Name: %s\nLast Name: %s\nGrade: %3.2f\n", current->id, current->fname, current->lname, current->grade);
current = current->next;
}
}
void main() {
FILE *p = fopen("students.txt", "r");
int choice, id, A = 0;
Node *head = NULL, *newStd = NULL, *cptr = NULL, *current;
char fname[20], lname[20];
if(p != NULL) {
while(!feof( p )) {
newStd = (Node*)malloc(sizeof(Node));
if(newStd != NULL) {
fscanf(p, "%d%s%s%f", &newStd->id, newStd->fname, newStd->lname, &newStd->grade);
newStd->next = NULL;
if(head == NULL)
head = newStd;
else
cptr->next = newStd;
cptr = newStd;
} else
printf("No space available");
}
fclose(p);
while(A == 0) {
printf("1. Search for student using ID number\n2. Search for student using full name\n");
printf("3. Change a student's grade\n4. Print out all students with their info\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Please enter the ID of the student\n");
scanf("%d", &id);
findByID(id, head);
break;
case 2:
printf("Please enter the full name of the student\n");
scanf("%s%s", fname, lname);
findByName(fname, lname, head);
break;
case 3:
printf("Enter the ID of the student you want to update\n");
scanf("%d", &id);
updateGrade(id, head);
break;
case 4:
printAll(head);
break;
default:
printf("That's not a valid input!\n");
break;
}
printf("\n\n\n\n\nPress 0 to return back to the menu.\n");
scanf("%d", &A);
}
} else
printf("File cannot be opened");
}
P.S: The findByName() function still doesn't work. I would really appreciate it if someone would help me out sort this problem.