Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define NAME_SIZE 40
- #define MAX_ASSIGNMENT_SIZE 4096
- #define FILENAME_SIZE 44
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdbool.h>
- typedef struct {
- char name[NAME_SIZE];
- int zNumber;
- char assignment[MAX_ASSIGNMENT_SIZE];
- double score;
- } Student;
- /******
- Following functions will be implemented:
- For the Student:
- submitAssignment();
- getScore();
- studentPrint();
- For the Teacher:
- getAssignment();
- assignScore();
- ********/
- void welcomeScreen(Student* pStudent);
- void submitAssignment(Student* pStudent);
- void print(Student* pStudent);
- void intialize(Student* pStudent, char name[], int zNumber);
- double getScore(Student* pStudent);
- void getAssignment(Student* pStudent);
- void assignScore(Student* pStudent, double score);
- void submitAssignment(Student* pStudent) {
- char studentFilename[FILENAME_SIZE];
- char assignment[MAX_ASSIGNMENT_SIZE] = "";
- printf("What is your assignment name?\n");
- scanf(" %4096s", assignment);
- strcpy(pStudent->assignment, assignment);
- FILE *outfile;
- // open file for writing
- outfile = fopen("class.txt", "w");
- if (outfile == NULL) {
- fprintf(stderr, "\nError opening file.\n");
- exit(1);
- }
- fprintf(outfile, " %s", pStudent->name);
- fprintf(outfile, " %d", pStudent->zNumber);
- fprintf(outfile, " %s", pStudent->assignment);
- fprintf(outfile, " %lf", pStudent->score);
- fprintf(outfile, "%s\n", "zzzzzz");
- fclose(outfile);
- }
- double getScore(Student* pStudent) {
- return(pStudent->score);
- }
- void getAssignment(Student* pStudent) {
- double studentScore;
- //Check if student has submitted assignment
- if (!strcmp(pStudent->assignment, "None")) {
- printf("Student has not submitted assignment. \n");
- }
- else {
- printf("What is this assignments grade?\n");
- scanf(" %lf", &studentScore);
- assignScore(pStudent, studentScore);
- }
- }
- void assignScore(Student* pStudent, double score) {
- char studentFilename[FILENAME_SIZE];
- pStudent->score = score;
- strcpy(studentFilename, pStudent->name);
- strcat(studentFilename, ".txt");
- FILE *outfile;
- // open file for writing
- outfile = fopen(studentFilename, "w");
- if (outfile == NULL) {
- fprintf(stderr, "\nError opening file.\n");
- exit(1);
- }
- fprintf(outfile, " %s", pStudent->name);
- fprintf(outfile, " %d", pStudent->zNumber);
- fprintf(outfile, " %s", pStudent->assignment);
- fprintf(outfile, " %lf", pStudent->score);
- fclose(outfile);
- }
- void print(Student* pStudent) {
- printf("%s\n", pStudent->name);
- printf("%d\n", pStudent->zNumber);
- printf("%s\n", pStudent->assignment);
- printf("%lf\n", pStudent->score);
- printf("\n");
- }
- void intialize(Student* pStudent, char name[], int zNumber) {
- strcpy(pStudent->name, name);
- pStudent->zNumber = zNumber;
- pStudent->score = -1.0; // -1 => assignment not graded yet.
- }
- /*void test(Student* pStudent) {
- //strcpy(pStudent->name,"Peter");
- intialize(pStudent, "Peter", 12345678);
- //pStudent->zNumber=12345678;
- submitAssignment(pStudent);
- //strcpy(pStudent->assignment,"This is a sample assignment");
- //pStudent->score=0.0;
- print(pStudent);
- } */
- int main(int argc, char **argv)
- {
- Student aStudent;
- welcomeScreen(&aStudent);
- // test(&aStudent);
- return(0);
- }
- void welcomeScreen(Student *pStudent)
- {
- char personType;
- char studentName[NAME_SIZE];
- char studentFilename[FILENAME_SIZE];
- int newNumber;
- int selection;
- char studentAssignment;
- char string[50];
- //Check to see if this is a Student or Teacher using the program.
- printf("Hello! Are you a student (s) or a teacher (t)? \n");
- scanf(" %c", &personType);
- //If it's a student.
- if ((personType == 's') || (personType == 'S')) {
- bool studentExists = false;
- printf("What is your name? \n");
- scanf(" %s", studentName);
- FILE *fp;
- fp = fopen("class.txt", "r");
- if ((fp = fopen("class.txt", "r")) == NULL) {
- printf("Error! opening file");
- // Program exits if file pointer returns NULL.
- exit(1);
- }
- while ((!studentExists) && (fp == EOF)) {
- // reads text until newline
- fscanf(fp, "%[^\n]", string);
- //debugging only broo
- printf("Data from the file:\n%s", string);
- if (string == studentName) {
- studentExists = true;
- }
- }
- if (!studentExists) {
- fclose(fp);
- }
- //Student doesnt exits
- if (!studentExists) {
- //Give greetings to the new student and check if they have an assignment to add or just registering.
- printf("Welcome new student! Let's create a record file for you.\n");
- printf("What is your zNumber?\n");
- scanf(" %d", &newNumber);
- intialize(pStudent, studentName, newNumber);
- printf("Here is your information:\n");
- print(pStudent);
- printf("Do you have an assignment to turn in? (Y or N)\n");
- scanf(" %c", &studentAssignment);
- if ((studentAssignment == 'y') || (studentAssignment == 'Y')) {
- submitAssignment(pStudent);
- }
- if ((studentAssignment == 'n') || (studentAssignment == 'N')) {
- printf("Thank you! Your student file has been created. You can come back later to submit your assignment.\n");
- strcpy(pStudent->assignment, "None");
- }
- FILE *outfile;
- // open file for writing
- outfile = fopen("class.txt", "w");
- if (outfile == NULL) {
- fprintf(stderr, "\nError opening file.\n");
- exit(1);
- }
- fprintf(outfile, " %s", pStudent->name);
- fprintf(outfile, " %d", pStudent->zNumber);
- fprintf(outfile, " %s", pStudent->assignment);
- fprintf(outfile, " %lf", pStudent->score);
- fprintf(outfile, "%s\n", "zzzzzz");
- fclose(fp);
- }
- else {
- //Student exists
- printf("Welcome back %s!\n", studentName);
- fscanf(fp, "%s %d %s %lf", pStudent->name, &pStudent->zNumber, pStudent->assignment, &pStudent->score);
- fclose(fp);
- printf("Would you like to check on a grade (1), print your information (2), or submit an assignment (3)?");
- scanf(" %d", &selection);
- if (selection == 1) {
- if (pStudent->score > -1) {
- printf("Your grade is %lf", getScore(pStudent));
- }
- else {
- printf("Your assignment has not yet been graded!\n");
- }
- }
- else if (selection == 2) {
- print(pStudent);
- }
- else if (selection == 3) {
- submitAssignment(pStudent);
- }
- }
- }
- else if ((personType == 't') || (personType == 'T')) {
- printf("Greetings Professor!\n");
- printf("Enter a students name to grade their assignment:\n");
- scanf(" %s", studentName);
- FILE *fp;
- strcpy(studentFilename, studentName);
- strcat(studentFilename, ".txt");
- fp = fopen(studentFilename, "r");
- fscanf(fp, "%s %d %s %lf", pStudent->name, &pStudent->zNumber, pStudent->assignment, &pStudent->score);
- fclose(fp);
- getAssignment(pStudent);
- print(pStudent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement