cs201 assignment 3 complete and exect right solution









complete code

//----------------------------------------------------------------------------------------------------------------
// DECLARING HEADER FILES
//----------------------------------------------------------------------------------------------------------------

// Declaring the C++ header file to use Input and Output Functions in the program (e.g. cin, cout)
#include <iostream>
// Declaring the C++ header file to use String Functions in the program (e.g. string keyword, strcpy)
#include <string>
// Declaring the C header file to use the C Language's String Functions in the program (e.g. strchr)
#include <cstring>
// Declaring the C++ header file to use Input/Output Manipulation Functions (e.g. setw)
#include <iomanip>

//----------------------------------------------------------------------------------------------------------------

// This statement is used to use the standard names for some functions which are updated in C++ and old names are not used by most compilers
using namespace std;


//----------------------------------------------------------------------------------------------------------------
// STEP 4: Use structure name "course" in your program.
//----------------------------------------------------------------------------------------------------------------

// Declaring the structure for students' information
struct course;
// Defining the structure
struct course
{
// Declaring the Structure's Elements that contains course name, course code, semester and degree's current year
char courseName[50]; // for storing Course Name
char courseCode[10]; // for stroring Course Code
char sem[50]; // for storing Semester Detail
char currentYear[10]; // for storing the Current Year
};


//----------------------------------------------------------------------------------------------------------------
// STEP 5: Use separate functions for taking user inputs, displaying and formatting outputs. 
//   e.g. InputCourses(),DisplayHeader(),DisplayCourses(),FormatCourse(---) etc.
//----------------------------------------------------------------------------------------------------------------

// Declaring the function to get input of courses
char getInputCourses(char arr[62], char arr2[62], course s[6], int n);

// Declaring the function to display header at output screen
char dispTopHeader();

// Declaring the function to display formatted courses
char dispFormattedCourses(int m);

// Defining the 'getInputCourses' Function
char getInputCourses(char arr[62], char arr2[62], course s[6], int n)
{
// Declaring token pointers to determine the first white space in the string of course information and semester information
char *coursePtr; // Token pointer for separating Course name and Course Code from the first string taken from user
char *semPtr; // Token pointer for separating Semester and Degree's Current Year
//----------------------------------------------------------------------------------------------------------------
// STEP 3: The program should input data for five courses and semester.
//----------------------------------------------------------------------------------------------------------------
// This for loops runs and takes the data of only 5 Courses and Semesters
for (int i = 1; i < n; i++)
{
//----------------------------------------------------------------------------------------------------------------
//STEP 1: Your program should provide the user with options to enter data for courses along with course code. e.g.
//        Enter course name along with course code: CS201 Introduction to Programming
//----------------------------------------------------------------------------------------------------------------
// Asks user to enter the course code along with name
cout << "Please enter the code and name of your course:\t" ;
// Takes the user input of course code and assign it to the array-1
cin.getline(arr, 62, '\n');
// This pointer token starts reading the input from array-1 given by user and stops at the place where the first 'white space' comes
coursePtr = strchr(arr, ' ');
// This if statement runs when the above token has finally reached at 'white space'
if (coursePtr)
{
// After token reached at white space in array1, this statement puts the null character there to separate the string and save the read string and leave the left
*coursePtr = '\0';
// This function copes the string currently present in the above pointer token to the 'courseCode' element of the structure we defined at start
strcpy(s[i].courseCode, arr);
// This statement makes increment in token that makes it leave the string which was read already and now reads the next part of string which was left before
coursePtr++;
// This function copies the new string from the token to 'courseName' element of the structure we defined at start
strcpy(s[i].courseName, coursePtr);
// If statement ends
}
//----------------------------------------------------------------------------------------------------------------
//STEP 2: After taking course information from user your program should prompt user to enter semester information.
//        e.g. Enter Semester: Fall 2016
//----------------------------------------------------------------------------------------------------------------
// Asks user to enter the semester and the current degree year
cout << "Please enter your semester\t ";
// Takes the user input of course code and assign it to the array-2
cin.getline(arr2, 62, '\n');
// This function copies the whole string from array-2 to 'sem' element of the structure we defined at start
strcpy(s[i].sem, arr2);
// This pointer token starts reading the input from array-2 given by user and stops at the place where the first 'white space' comes
semPtr = strchr(arr2, ' ');
// This if statement runs when the above token has finally reached at 'white space'
if (semPtr)
{
// After token reached at white space in array-2, this statement puts the null character there to separate the string and save the read string and leave the left
*semPtr = '\0';
// This statement makes increment in token that makes it leave the string which was read already and now reads the next part of string which was left before
semPtr++;
// This function copies the new string from the token to 'currentYear' element of the structure we defined at start
strcpy(s[i].currentYear, semPtr);
// If statement ends
}
// For loop ends
}
// Leave some lines after taking the input from user for well organized things
cout << endl << endl << endl;
// Definition of function 'getInputCourses' ends
}


// Definition of function 'dsipTopHeader'
char dispTopHeader()
{
// STEP 3. The program should input data for five courses and semester.
// Displays the top header with proper width b/w elements
cout << "#" << setw(17) << "Course Code" << setw(27) << "Course Name" << setw(27) << "Semester" << setw(22) << "Launching Year\n";
// Displays the separator-line after the header
cout << "_____________________________________________________________________________________________" << endl << endl;
// Definition of function 'dispTopHeader' ends
}

// Definition of Function 'dispFormattedCourses'
char dispFormattedCourses(int z, course su[5])
{
// This for loop circulates through the arrays in which input has stored and prints each cycle of input on screen separating them by a line
for (int i = 1; i < z; i++)
{
// These statements prints the input (taken from user) in a specific table format
cout.width(10); cout << left << i; // cout.width is a function we can use to fix the width from the current position of any character
cout.width(17); cout << left << su[i].courseCode;
cout.width(36); cout << left << su[i].courseName;
cout.width(20); cout << left << su[i].sem;
cout.width(23); cout << left << su[i].currentYear;
cout << endl;
// For loop ends
}
// Definition of Function 'dispFormattedCourses' ends
}

// The Main Function starts here
int main()
{
//----------------------------------------------------------------------------------------------------------------
// Declaring and Defining a variable 'maxCourses' to use it in the loop to take the data of only 5 Courses
//----------------------------------------------------------------------------------------------------------------
int maxCourses = 6; // The value is set to 6 because loop has to be run for both array 'cr' of 'course' type and for the max limit of coruses, so if we put 6, we can
                   // start the counter from 1 and put the limit to less than 6, which gives us exactly 5 limit and we can ignore the 0th index of the array 'cr', while
                   // displaying the output.
                   
// Declaring arrays to assign the input taken by user repectively
char courseInfo[62]; // Array size calculated by adding the size of courseName(i.e. 50), courseCode(i.e. 10), space(i.e. 1) and the null character(i.e. 1)
char semStartingDetail[62]; // Array size calculated by adding the size of courseName(i.e. 50), courseCode(i.e. 10), space(i.e. 1) and the null character(i.e. 1)
//----------------------------------------------------------------------------------------------------------------
// Declaring an array 'cr' of 'course' type with 6 size to make the structure available for 5 courses and semesters
//----------------------------------------------------------------------------------------------------------------
course cr[6]; // Again the same thing I told above that the, 0th element of the array will not be used to print out and also will
             // neither be used to store the date nor be printed out on screen, it will just be ignored and will be empty.
// Calling the function to get the input from user of Courses and Semesters details
getInputCourses(courseInfo, semStartingDetail, cr, maxCourses);
// Calling the function to print the top header on the screen after the program has taken the input and stored it in proper place
dispTopHeader();
// Caaling the function to show the taken input on screen in well-formatted table form
dispFormattedCourses(maxCourses, cr);
// Leave some lines after displaying the input in well-formatted table form on screen, to make the table prominent
cout << endl << endl << endl;
// The Main Function ends
}