I finally had a chance to work on the program since I'm having exams. I got some things done but im having difficulty with it printing the GPA and storing multiple grades in the index. When i ran it before i input the gpa in subchoice 4, it printed the name of the student i would input but didn't print the grade. And im not sure how it would be best to do the GPA because i know its the average of however many grades are inputted but im just not sure how to write it in code, as in whether it should be looped multiple times.
Code:
#define _CRT_SECURE_NO_WARNINGS
#define RECORDS_SIZE 1000
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
char fname[100], lname[100];
double grades[100];
int ID;
}student;
int a = 0;
int j = 0;
int i = 0;
int choice = 0;
int subchoice = 0;
char username[100];
char password[100];
char fusername[100], fpassword[100];
void AuthorInfo()
{
printf("Timofey Kononets\n2651763\n");
}
// result is 0 for a clean exit, nonzero for an exit forced by error
void KillProgram(int result)
{
exit(result);
}
void DoLogin()
{
while (1)
{
printf("Press 0 to exit\nPress 1 for author info\nPress 2 to Log in\n");
scanf("%d", &choice);
if (choice == 0)
KillProgram(0);
if (choice == 1)
AuthorInfo();
if (choice == 2)
{
FILE *file = fopen("passwords1.txt", "r");
if (file == NULL)
{
printf("Can't open passwords file!!\nKilling program\n");
KillProgram(1);
}
printf("Enter a user name\n");
scanf("%s", username);
printf("Enter password\n");
scanf("%s", password);
while (fscanf(file, "%s%s", fusername, fpassword) != EOF)
{
if (strcmp(username, fusername) == 0)
{
if (strcmp(password, fpassword) == 0)
{
printf("Login successful\n");
fclose(file);
return; //end loop and go back to main
}
else
{
printf("Login unsuccessful\n");
}
}
}
fclose(file);
}
}
}
void DoSecondMenu(student records[])
{
while (2)
{
printf("Press 1 for author info\nPress 2 to enter new student\nPress 3 to enter grade for existing student\nPress 4 to print student records\nPress 5 to save student records\nPress 6 to load student records\nPress 7 to logout\n");
scanf("%d", &subchoice);
if (subchoice == 1)
{
printf("Timofey Kononets\n2651763\n");
}
else if (subchoice == 2)
{
printf("Enter student first name\n");
scanf("%s", records[i].fname);
printf("Enter student last name\n");
scanf("%s", records[i].lname);
i++;
}
else if (subchoice == 3)
{
printf("Enter student ID\n");
scanf("%d", &j);
printf("Enter a numeric value for the gradepoint in this class\n");
scanf("%lf", records[j].grades[a]);
j++;
a++;
}
else if (subchoice == 4)
{
for (int k = 0; k < i; k++)
{
printf("First name: %s Last Name: %s", records[k].fname, records[k].lname);
for (k = 0; k < j; k++)
{
printf("GPA: %lf", records[k].GPA / records[k].grades[a]);
}
}
}
}
}
int main()
{
student student_records[RECORDS_SIZE] = { 0 };
DoLogin();
DoSecondMenu(student_records);
return 0;
}