Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Given this code: #include #include #include #include #define NUM_COURSES 5 int main(void) { int selected_courses[NUM_COURSES]; int num_selected_courses = 0; int c; const char * const
Given this code:
#include #include #include #include #define NUM_COURSES 5 int main(void) { 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 { bool input_valid = false; 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 = true; 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 if ( num_selected_courses == NUM_COURSES ) break; //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' ); printf( "You have selected the following courses: " ); //print courses selected by user for ( int i = 0; i < num_selected_courses; i++ ) printf( "%s ", list_courses[selected_courses[i]] ); }
________________
How can the program show the list of courses, user selects some courses and add to a list.The list is then added to an array named courses for eg
struct Students{ char first_name[100]; char last_name[100]; int age; char address[100]; char programme[100]; char courses[100]; // The list of courses the user inputted needs to be stored here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started