I have to write a menu UI program that adds student's name, score, average, letter grade, show all data for 1 particular student, and show all the data. the name has to be a string, id is a string of 4, and score is an int array of 4. The program is to be implemented using linked-list.
This is what I got so far and it's not looking good... We had a similar assignment but using structures instead of linked-lists and I attempted to convert it with no luck
Code:
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#define FLUSH while (getchar() != '\n')
#define M 3
#define N 4
typedef struct node *nd;
struct node
{
char sId[4];
char name[40];
int score[N];
char grade;
struct node *next;
}STUD;
void inputData(nd *head);
void inquireScores(nd head);
void inquireAverage(nd head);
void inquireGrade(nd head);
void inquireStudent(nd head);
void showAll(nd head);
int search(nd head);
void showScore(nd head);
void showName(nd head);
float calcAverage(nd head);
char getGrade(float);
void showError(void);
int main(void)
{
struct STUD *nd;
int ans,n;
inputData(&nd);
do{
system("cls");
printf("1] Student Score\n");
printf("2] Student Average:\n");
printf("3] Student Grade\n");
printf("4] Student Academic Record\n");
printf("5] Student List\n");
printf("6] Exit\n\n");
printf("Choice: ");
scanf("%d",&ans);
switch(ans)
{
case 1: inquireScores(nd);
break;
case 2: inquireAverage(nd);
break;
case 3: inquireGrade(nd);
break;
case 4: inquireStudent(nd);
break;
case 5: showAll(nd);
break;
case 6: printf("\n\nClosing program...");
sleep(3000);
break;
default: printf("\n\nInvalid choice...");
sleep(2000);
}
}while (ans != 6);
return 0;
}
void inputData(nd *head)
{
char sId[4];
char name[30];
nd t, t2, temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while(temp->next != NULL)
{
t = temp->next;
t2 = temp->next;
system("cls");
printf("ID: ");
strcpy(t->sId, sId);
printf("\nName: ");
strcpy(temp->name, name);
printf("\nscore [%d]: ");
scanf("%d",&t2->score);
FLUSH;
}
return;
}
void showName(nd head)
{
nd temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while(temp->next != NULL)
{
system("cls");
printf("Student ID: %s\n",temp->sId);
printf("Student Name: %s\n",temp->name);
}
return;
}
void showScore(nd head)
{
nd temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
printf("Scores: \n");
while(temp->next != NULL)
{
printf(" %d\n",temp->score);
}
return;
}
void showError(void)
{
printf("Student ID not found...");
getch();
return;
}
void inquireScores(nd head)
{
char name[30];
nd temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while (temp != NULL && strcmp(temp->name, name) != 0)
{
temp = temp->next;
}
if (temp == NULL)
{
showError();
return;
}
else
{
showName(head);
showScore(head);
getch();
}
return;
}
void inquireAverage(nd head)
{
nd temp;
char name[30],gr;
float avg;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while (temp != NULL && strcmp(temp->name, name) != 0)
{
temp = temp->next;
}
if (temp == NULL)
{
showError();
return;
}
else
{
avg = calcAverage(head);
showName(head);
printf("Average: %0.2f",avg);
getch();
}
return;
}
void inquireGrade(nd head)
{
nd temp;
char name[30],gr;
float avg;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while (temp != NULL && strcmp(temp->name, name) != 0)
{
temp = temp->next;
}
if (temp == NULL)
{
showError();
return;
}
else
{
avg = calcAverage(head);
gr = getGrade(avg);
showName(head);
printf("\nGrade: %c",gr);
getch();
}
return;
}
char getGrade(float avg)
{
char gr;
if (avg >= 91)
gr = 'A';
else if ((gr >= 81) && (gr <= 90))
gr = 'B';
else if ((gr >= 71) && (gr <= 80))
gr = 'C';
else if ((gr >= 61) && (gr <= 70))
gr = 'P';
else
gr = 'F';
return gr;
}
float calcAverage(nd head)
{
nd temp;
temp=head;
int sum = 0, num;
float avg;
while ( temp->next != NULL )
{
sum+=temp->score;
num+=1;
avg = (float) sum / num;
return avg;
}
}
int search(nd head)
{
int i,res = -1;
nd temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
char id[5];
FLUSH;
printf("ID: ");
gets(temp -> sId);
while(temp->next != NULL)
{
if (strcmp(temp->sId,temp->next->sId) == 0)
{
res = i;
break;
}
}
return res;
}
void inquireStudent(nd head)
{
nd temp;
char name[30],gr;
float avg;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
while (temp != NULL && strcmp(temp->name, name) != 0)
{
temp = temp->next;
}
if (temp == NULL)
{
showError();
return;
}
else
{
showName(head);
showScore(head);
avg = calcAverage(head);
printf("\nAverage: %0.2f",avg);
gr = getGrade(avg);
printf("\nGrade: %c",gr);
getch();
}
return;
}
void showAll(nd head)
{
nd temp;
temp=malloc(sizeof(STUD));
temp -> next = NULL;
if (head==NULL)
printf ("No elements to print!\n");
else {
temp=head;
do {
system("cls");
printf("Student List:\n\n");
printf("Student ID\tStudent Name\n\n");
printf("%s\t\t%s\n",temp -> sId, temp -> name);
temp=temp->next;
} while (temp!=NULL);
}
getch();
return;
}