1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// 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>
// 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];
char courseCode[10];
int srNo[5];
char sem[50];
char currentYear[10];
};
/*
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[5]);
// Declaring the function to display header at output screen
char dispTopHeader();
// Declaring the function to display formatted courses
char dispFormattedCourses();
// Defining the function to get input of courses
char getInputCourses(char arr[62], char arr2[62], course s[5])
{
// Declaring tokens for to determine the first white space in the string of course information and semester information
char *coursePtr; // Token for separating Course name and Course Code from the first string taken from user
char *semPtr; // Token for separating Semester and Degree's Current Year
// Declaring and Defining a variable 'maxStuds' to use it in the loop to make the data limited to 5 students only
int maxStuds = 5;
// This for loops runs and make the limit of students entering their data to 5 only
for (int i = 0; i <= maxStuds; i++)
{
// Asks user to enter the course code along with name
cout << "Please enter the code and name of your course\n" << "(e.g. CS201 Introduction to Programming): ";
// Takes the user input of course code and assign it to the array1
cin.getline(arr, 62, '\n');
// This pointer token starts reading the input from array1 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
}
// Asks user to enter the semester and the current degree year
cout << "Please enter your semester\n" << "(e.g. Fall 2016): ";
// Takes the user input of course code and assign it to the array2
cin.getline(arr2, 62, '\n');
// This function copies the whole string from array2 to 'sem' element of the structure we defined at start
strcpy(s[i].sem, arr2);
// This pointer token starts reading the input from array2 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 array2, 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
}
// Definition of function 'getInputCourses' ends
}
// The main function starts here
int main()
{
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)
course stud[5];
getInputCourses(courseInfo, semStartingDetail, stud);
}
|