Could you complete the incomplete code below without adding any header file? ---
ID: 3590854 • Letter: C
Question
Could you complete the incomplete code below without adding any header file?
--------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct Student {
string firstName;
string lastName;
int id;
};
class Course {
private:
string name;
int crn;
int section;
Student students[100];
int numStudents;
public:
Course() {
}
Course(string n, int c, int s) {
name = n;
crn = c;
section = s;
numStudents = 0;
}
void modifyInfo(string n, int c, int s) {
name = n;
crn = c;
section = s;
numStudents = 0;
}
int getCrn() {
return crn;
}
void addStudent(Student stu) {
students[numStudents].firstName = stu.firstName;
students[numStudents].lastName = stu.lastName;
students[numStudents].id = stu.id;
numStudents++;
}
};
void load(Course schedule[100], int& numCourses);
void print(Course schedule[100], int numCourses);
int main() {
// init vars
Course schedule[100];
int numCourses = 0;
char sc = 'a';
// menu loop
while(sc != 'e') {
// prompt and get menu choice
cout << endl << endl << endl
<< "Would you like to load, print, add a student, add a course, or exit (l/p/s/c/e): ";
cin >> sc;
// choose function based on user input
switch(sc) {
case 'e':
cout << "Thanks for using the roster program!";
break;
case 'l':
load(schedule, numCourses);
break;
case 'p':
print(schedule);
break;
case 'c':
string name;
int crn, section;
cout << "Please enter the course name, crn and section: ";
cin >> name >> crn >> section;
schedule[numCourses].modifyInfo(name, crn, section);
numCourses++;
break;
case 's':
// get the crn of the course
cout << "Please enter the crn of the course you'd like to add a student to: ";
int crn;
cin >> crn;
// find which course in the array that corresponds to that crn
// create the student and add the student that course
break;
default:
cout << "That was not a valid option, please try again!";
}
}
return 0;
}
// this function gets the file path from the user
// then opens the file, reads it with a loop,
// and saves the data read from the file into the array
void load(Course schedule[100], int numCourses) {
// does course exist already?
// if so, add student to it
// if not, create the course and add the student
}
// this function prints the contents of the array
// prettily using the setw function (#include <iomanip>)
void print(Course schedule[100], int numCourses) {
}
Explanation / Answer
/* Hi Please find code below and comment in case if you have something to ask .
Thanks for opportunity */
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <vector>
/*
create a file test.txt in where u use this source code.
means you source code file and test.txt file should be in same folder
you need to write the test.txt file in same order with comma(,) seperated
name crn section numberofstudent Student
computer,1,1,2,amit,sumit
science,2,2,2,amit,sumit
*/
using namespace std;
struct Student {
string firstName;
string lastName;
int id;
public:
void setfirstName(string name){
firstName = name;
}
void setlastName(string lname){
lastName = lname;
}
void setId(int x){
id=x;
}
string getfirstName(){ return firstName;}
string getlastname() { return lastName;}
int getId(){ return id;}
};
class Course {
private:
string name;
int crn;
int section;
int numStudents;
Student students[100];
public:
Course() {
}
Course(string n, int c, int s) {
name = n;
crn = c;
section = s;
numStudents = 0;
}
void modifyInfo(string n, int c, int s) {
name = n;
crn = c;
section = s;
numStudents = 0;
}
int getCrn() {
return crn;
}
int getSection(){
return section;
}
int getnumStudents(){
return numStudents;
}
string getname(){
return name;
}
string getstudent(int numberofstudent){
int i=0;
string temp="";
for(i=0;i<numberofstudent;i++)
temp += students[i].getfirstName()+" "+students[i].getlastname()+",";
return temp;
}
void addStudent(Student stu) {
students[numStudents].firstName = stu.firstName;
students[numStudents].lastName = stu.lastName;
students[numStudents].id = stu.id;
numStudents++;
}
void setname(string n)
{name =n;}
void setcrn(int n){crn =n;}
void setsection(int n){section =n;}
void setnumStudents(int n){numStudents =n;}
void setStudents(vector<string> stuname){
int index=0;
for (std::vector<string>::iterator it = stuname.begin() ; it != stuname.end(); ++it){
students[index].setId(index+1);
istringstream iss(*it);
string subs;
iss >> subs; // read the firstname
students[index].setfirstName(subs);
iss >> subs; // read the last name
//below if avoid if no last given in test.txt
if(subs.compare(students[index].getfirstName())) {
students[index].setlastName(subs);
}
index++;
}
}
};
void load(Course schedule[100], int &numCourses);
void print(Course schedule[100], int numCourses);
int main() {
Course schedule[100];
int numCourses = 0;
char sc = 'a';
while(sc != 'e') {
cout << endl << endl << endl
<< "Would you like to load, print, add a student, add a course, or exit (l/p/s/c/e): ";
cin >> sc;
// choose function based on user input
switch(sc) {
case 'e':
cout << "Thanks for using the roster program!";
break;
case 'l':
load(schedule, numCourses);
break;
case 'p':
print(schedule,numCourses);
break;
case 'c':
string name;
int crn, section;
cout << "Please enter the course name, crn and section: ";
cin >> name >> crn >> section;
schedule[numCourses].modifyInfo(name, crn, section);
numCourses++;
break;
// case '':
// get the crn of the course
// int crn;
// cout << "Please enter the crn of the course you'd like to add a student to: ";
// cin >> crn;
/*
findcourse(Course schedule,int crn){
int i=0;
for(i=0;i<100;i++){
if(schedule[i].getCrn()==crn){
}
}
}
*/
// find which course in the array that corresponds to that crn
// create the student and add the student that course
// break;
// default:
// cout << "That was not a valid option, please try again!";
}
}
return 0;
}
// this function gets the file path from the user
// then opens the file, reads it with a loop,
// and saves the data read from the file into the array
void load(Course schedule[100], int &numCourses) {
// does course exist already?
if(numCourses !=0){
cout<<"data is all ready loaded so no need to read from system"<<endl;
return ;
}
// create the test.txt file in same directory where source code is present
// adding the data in the source directory in that order
// each line is seperated by /n or next line character
//name crn Section numberofstudent Student
//computer,1,1,3,amit,sumit,arya stark
//science,2,2,2,amit,sumit
//maths,3,4,3,jhon,snow targeyeran,bryan
ifstream infile( "test.txt" );
if(!infile.good()){
cout<<" test.txt file is missing in current directory "<<endl;
return;
}
cout << "Data is loaded from the test.txt file"<<endl;
while (infile)
{
vector<string> stuname;
string s;
if (!getline( infile, s )) break;
istringstream ss( s );
int i=1;
while (ss) {
//outer line read number of line exsit in test.txt
string s;
// Reading the value one by one ....with comma(,) as a delimiter
if (!getline( ss, s, ',' )) break;
if (i==1) // first value is always name of subject in a line
schedule[numCourses].setname(s);
if (i==2)// second value is always be crn
schedule[numCourses].setcrn(atoi(s.c_str()));
if (i==3)// thrid value is always be a section
schedule[numCourses].setsection(atoi(s.c_str()));
if (i==4)//fourth value is always be a number of student
schedule[numCourses].setnumStudents(atoi(s.c_str()));
if (i>4) // fifth onwards is the name of the student (it varies may be 1 or two)
// adding first name and last name to the s
// reading in that order name,crn,section,numStudents,students
stuname.push_back(s);
i++;
}//end if inner while loop , run per lined based
schedule[numCourses].setStudents(stuname); //
numCourses++;
}//end of file reached
if (!infile.eof())
{
cerr << "closing file! ";
infile.close();// close the file pointer
}
// if so, add student to it
// if not, create the course and add the student
}
// this function prints the contents of the array
// prettily using the setw function (#include <iomanip>)
void print(Course schedule[100], int numCourses) {
int i=0;
cout<<"Total number of course:"<<numCourses<<endl;
for(i=0;i<numCourses;i++){
cout<<"SubjectName:"<<schedule[i].getname()<<endl;
cout<<"CRN:"<<schedule[i].getCrn()<<endl;
cout<<"Section:"<<schedule[i].getSection()<<endl;
cout<<"List of Student:"<<schedule[i].getnumStudents()<<endl;
cout<<"Number of Student:"<<schedule[i].getstudent(schedule[i].getnumStudents())<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.