Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program currently only asks the user to input courses, I want the list of courses the user entered to be stored in struct Students{

The program currently only asks the user to input courses, I want the list of courses the user entered to be stored in struct Students{ char courses[NUM_COURSES][100];. The list of courses the user entered should be able to be printed by using printf("Courses %s ",temp->courses); #include #include #include #include  #define NUM_COURSES 5 int i = 0; struct Students{ char first_name[100]; char last_name[100]; int age; char address[100]; char programme[100]; char courses[NUM_COURSES][100]; int num_selected_courses; struct Students *next; }*head; void enter_student(char* first_name, char* last_name, int age, char* address, char* programme,char* courses,int num_selected_courses) { struct Students * student = (struct Students *) malloc(sizeof(struct Students)); strcpy(student->first_name, first_name); strcpy(student->last_name, last_name); student->age = age; strcpy(student->address, address); strcpy(student->programme, programme); strcpy(student->courses[i], courses); student-> num_selected_courses= num_selected_courses; student->next = NULL; if(head==NULL){ // if head is NULL // set student as the new head head = student; } else{ // if list is not empty // insert student in beginning of head student->next = head; head = student; } } void search_fname(char* first_name, char* last_name, int age, char* address, char* programme,char*courses,int num_selected_courses) { struct Students * temp = head; char a[50]; int j; printf("Enter First Name of Student to Search: "); scanf("%s", &a); while(temp!=NULL){ for (int j = 1; j <= i; j++) { if (!strcmp(first_name, a)){ printf("The Students Details are: "); printf("First Name: %s ", temp ->first_name); printf("Last Name: %s ", temp->last_name); printf("Age:%d ", temp->age); printf("Address: %s ", temp->address); printf("Programme: %s ", temp->programme); printf("Courses %s ", temp ->courses); /// list of courses for student name entered prints here return; } } temp = temp->next; } printf("Student name %s not found!!! ", a); } int main() { struct Students stud; head = NULL; char first_name[100]; char last_name[100]; int age; char address[100]; char programme[100]; int selected_courses[NUM_COURSES]; int num_selected_courses = 0; int c; const char * const list_courses[NUM_COURSES] = { "CSE1100", "CSE1101", "CSE1102", "ITE1100", "ITE1101" }; do { int input_valid = 0; int selected_course; int d; while ( !input_valid ) { char input[100]; //prompt user for input printf( "Courses available: " " CSE1100 " " CSE1101 " " CSE1102 " " ITE1100 " " ITE1101 " " " "Enter Course: " ); //attempt to read one word of user input if ( scanf( "%99s", input ) != 1 ) { fprintf( stderr, "unexpected input failure! " ); exit( EXIT_FAILURE ); } //discard remainder of input line do { d = getchar(); } while ( d != ' ' && d != EOF ); //determine whether course entered was valid or not for( int i = 0; i < NUM_COURSES; i++ ) { if ( strcmp(input, list_courses[i] ) == 0) { input_valid = 1; selected_course = i; break; } } //if invalid, print error message if ( !input_valid ) printf( "Invalid choice! Try again. " ); } //input was valid, so add course selected_courses[num_selected_courses] = selected_course; num_selected_courses++; //if we have already reached the maximum number of //courses, then don't ask again //ask user whether he wants to add another course printf("Would you like to enter another course? (y or n) "); c = d = getchar(); //add spacing printf( " " ); //discard remainder of input line while ( d != ' ' && d != EOF ) d = getchar(); } while ( c == 'y' ); for ( int i = 0; i < num_selected_courses; i++ ) { //COPYING A SELECTED COURSE INTO STUDENT STRUCT strcpy(stud.courses[i], list_courses[selected_courses[i]]); } //POPULATE NUMBER OF SELECTED COURSES IN STUDENT STRUCT stud.num_selected_courses = num_selected_courses; printf( "You have selected the following courses: " ); //PRINTING ALL COURSES IN THE STUDENT STRUCT for ( int i = 0; i < stud.num_selected_courses; i++ ) //PRINTING A COURSE IN THE STUDENT STRUCT printf( "%s ", stud.courses[i] ); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago