I need help with these 2 C programs please. Thanks. Here is the header structs:
ID: 3732613 • Letter: I
Question
I need help with these 2 C programs please. Thanks.
Here is the header structs:
write a program called "schedule" which will schedule a course offering. To do this, you should prompt the user as shown below and allow them to provide the information for a course: /schedule Code: CIS 2500 Term: W2018 Title: Intermediate Programming Instructor: Stefan Kremer Your program should record the information provided in a course struct and then append it to the end of the "courses.bin" file. Generate your prompts exactly as shown (note, only one space after each colon write a program called "add" which will add a student to a course. To do this, you should prompt the user as shown below and allow them to provide the necessary information /add Student ID: 1062214 Course Code: CIS 2508 Term: W2018 Registration [CRD or AUD] YES Registration [CRD or AUD]: CRD If the user enters and invalid value for Registration, it should re-prompt until a valid value is provided. Generate your prompts exactly as shown (note, only one space after each colon). Initialize the grade value to -1. After recording the information in a student_course_struct, append it to the end of the "student_course.bin" file.Explanation / Answer
//Program 1: schedule.c
#include <stdio.h>
//declaring the structure
struct course_struct {
char code[10];
char term[6];
char title[128];
char instructor[64];
};
int main(){
struct course_struct cst;
printf("Code: ");
gets(cst.code);
printf("Term: ");
gets(cst.term);
printf("Title: ");
gets(cst.title);
printf("Instructor: ");
gets(cst.instructor);
FILE *ptr_fwrite;
ptr_fwrite = fopen("courses.bin","ab"); //opening file to write
if(!ptr_fwrite){
printf("Unable to open file to write. Program will exit");
return 1;
}
fwrite(&cst, sizeof(struct course_struct), 1, ptr_fwrite); //writing the record to file
fclose(ptr_fwrite); //closing file after writing record
return 0;
}
//Program 2: add.c
#include <stdio.h>
#include <string.h>
//declaring the structure
struct student_course_struct {
int student_id;
char course_code[10];
char term[6];
char registration[4];
int grade;
};
int main(){
struct student_course_struct cst;
printf("Student ID: ");
scanf("%d",&cst.student_id);
printf("Course Code: ");
scanf("%s",cst.course_code);
printf("Term: ");
scanf("%s",cst.term);
int flag = 1;
//checking if the user enters only CRD or AUD as input
while(flag){
printf("Registration [CRD or AUD]: ");
scanf("%s",cst.registration);
if(strcmp(cst.registration,"CRD")==0 || strcmp(cst.registration,"AUD")==0)
flag = 0;
}
cst.grade = -1;
FILE *ptr_fwrite;
ptr_fwrite = fopen("student_course.bin","ab"); //opening file to write
if(!ptr_fwrite){
printf("Unable to open file to write. Program will exit");
return 1;
}
fwrite(&cst, sizeof(struct student_course_struct), 1, ptr_fwrite); //writing to file
fclose(ptr_fwrite);
return 0;
}