I got a problem. When the fucntion DataEntry finished its job, it wont go back to main menu instead it go to the while part, which is Yes or No. Can someone help me find my mistake here?
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define SIZE 7
typedef struct GPA
{
char courseCode [20];
int credits;
float grade;
} ST;
int DataEntry (ST gpa [SIZE], int i);
void SearchEdit (ST gpa [SIZE], int i);
float CalGrade (ST gpa [SIZE], float grade, int i);
void Display (ST gpa [SIZE], int i, float grade);
int main (void)
{
int i = 0, ans;
ST gpa [SIZE];
float grade;
char res, ans1;
system ("cls");
do
{
printf ("MAIN MENU\n");
printf ("1. Add Course Code, Credits and Grades\n");
printf ("2. Search and Edit a Course\n");
printf ("3. Calculate the GPA and Display the Courses\n");
printf ("4. Exit\n\n");
printf ("Your Choice: ");
scanf ("%d", &ans);
switch(ans)
{
case 1:i=DataEntry (gpa, i);
break;
case 2: SearchEdit (gpa, i);
break;
case 3: CalGrade (gpa, grade, i);
Display;
break;
case 4:
exit(0);
break;
default: printf ("Wrong Choice");
}
printf ("\n\nRepeat (Y/N)? ");
scanf ("%c", &res);
}
while(res=='y'||res=='Y');
getch();
return 0;
}
int DataEntry (ST gpa [SIZE],int i)
{
char sub[20], ans1;
int cred, j;
float gra;
printf ("\n");
printf ("You can only input up to 7 courses.");
printf ("\nHow many courses: ");
scanf ("%d", &j);
for (i=0; i<j; i++)
{
fflush (stdin);
printf ("Enter Course Code: ");
gets(sub);
strcpy (gpa[i].courseCode, sub);
printf ("Enter Credits: ");
scanf ("%d",&cred);
gpa[i].credits = cred;
printf ("Enter Grade: ");
scanf ("%.1f", &gra);
gpa[i].grade = gra;
}
return i;
}
void SearchEdit (ST gpa[SIZE], int i)
{
char seco[20], newCourse[20];
int newCred, j;
float newGrade;
system ("cls");
printf ("Course to search: ");
gets (seco);
for (j=0; j<i; j++)
if (strcmp (gpa [j].courseCode, seco)==0)
{
printf ("FOUND!/n/n");
printf ("Enter new grade: ");
scanf ("%.1f", &newGrade);
gpa[j].grade = newGrade;
}
else
printf ("No Records Found!");
getch();
system("CLS");
}
float CalGrade (ST gpa [SIZE], float grade, int i)
{
float gr = 0, grr, cred=0;
int j;
for (j=0; j<i; j++)
{
grr = gpa[j].credits*gpa[j].grade;
gr+=grr;
}
for (j=0; j<i; j++)
{
cred+=gpa[j].credits;
}
grade = gr/cred;
return grade;
}
void Display (ST gpa [SIZE], int i, float grade)
{
int j;
system ("cls");
printf ("Course Code\t\tCredits\nGrade\n\n");
for (j=0;j<i;j++)
{
printf("%s\t\t",gpa[j].courseCode);
printf("%d\t%.1f\n"), gpa[j].credits, gpa[j].grade;
}
printf ("Your GPA is %.1f.", grade);
getch();
}