Trying to output in a certain way

Okay, so I am trying to have a specific output that I want to get, but it prints its out a bit differently.
And I wasn't too sure how to print it implement the setPrereqs into the printPrereqs function.
this is CourseType.h:
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
#ifndef COURSETYPE_H
#define COURSETYPE_H

#include <string>
#include <iostream>
#include <iomanip>
using namespace std;

class CourseType
{
public:
	CourseType();
	CourseType(const string& courseName, int courseNumber,
		double courseUnits);
	string getCourseName() const;
	int getCourseNumber() const;
	double getCourseUnits() const;
	string getPrefix() const;
	string setCourseName(string newCourseName);
	int setCourseNumber(int newCourseNumber);
	double setCourseUnits(double newCourseUnits);
	void printCourse() const;
	~CourseType();

private:
	string courseName;
	int courseNumber;
	double courseUnits;
};


#endif // !1 


this is CourseType.cpp:
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
#include "CourseType.h"
#include <iostream>


CourseType::CourseType()
{
	this->courseName = "No name assigned";
	this->courseNumber = 0;
	this->courseUnits = 0.00;
}

CourseType::CourseType(const string& courseName, int courseNumber,
	double courseUnits)
{
	this->courseName = courseName;
	this->courseNumber = courseNumber;
	this->courseUnits = courseUnits;
}
string CourseType::getCourseName() const
{
	return courseName;
}

int CourseType::getCourseNumber() const
{
	return courseNumber;
}

double CourseType::getCourseUnits() const
{
	return courseUnits;
}

string CourseType::getPrefix() const
{
	return "CS A";
}

string CourseType::setCourseName(string newCourseName)
{
	return this->courseName = newCourseName;
}

int CourseType::setCourseNumber(int newCourseNumber)
{
	return this->courseNumber = newCourseNumber;
}

double CourseType::setCourseUnits(double newCourseUnits)
{
	return this->courseUnits = newCourseUnits;
}

void CourseType::printCourse() const
{
	cout << fixed << showpoint << setprecision(2);
	cout << getPrefix() << this->courseNumber
		<< " - " << this->courseName << " ("
		<< this->courseUnits << " units)" << endl;
}

CourseType::~CourseType() {}



This is Course.h:
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
#ifndef COURSE_H
#define COURSE_H

#include "CourseType.h"
#include <vector>

class Course : public CourseType
{
public:
	Course();
	Course(const string& newName, int newNumber, double newUnits,
		const vector<int>& prereq, char transferable);
	char isTransferable() const;
	void setTransfer(char newTransferable);
	void setPrereqs(const int* prereqs, int numOfElem) const;
	void printCourse() const;
	void printPrereqs() const;
	~Course();

private:
	vector<int> prereq;
	char transferable;
};

#endif // !COURSE_H



This is Course.cpp:
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
#include "Course.h"

Course::Course()
{
	char transferable = 'N';
}

Course::Course(const string& newName, int newNumber, double newUnits,
	const vector<int>& prereq, char transferable)
	: CourseType(newName, newNumber, newUnits)
{
	//this->prereq.push_back = prereq.push_back;
	this->transferable = transferable;
}

char Course::isTransferable() const
{
	return this->transferable;
}

void Course::setTransfer(char newTransferable)
{
	this->transferable = newTransferable;
}

void Course::setPrereqs(const int* prereqs, int numOfElem) const
{
	if (numOfElem > 1)
	{
		for (int i = 0; i < numOfElem; i++)
		{
			cout << getPrefix();
			cout << prereqs[i] << " or ";
		}
		cout << endl;
	}

	else
		cout << getPrefix() << prereqs[1] << endl;
}

void Course::printCourse() const
{
	// CourseType::printCourse();
	cout << getPrefix() << getCourseNumber() << " - "
		<< getCourseName() << " (" << getCourseUnits()
		<< " units, " << ((this->transferable == 'Y') ? 
		("transferable") : ("not transferable")) << ")" << endl;
}

void Course::printPrereqs() const
{
	cout << getPrefix() << getCourseNumber() << " - "
		<< "Prerequisites: " << endl;
}

Course::~Course() {}




And this is the Main.cpp
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
#include "CourseType.h"		
#include "Course.h"

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	{
		cout << "*********************************************************\n"
			<< "            TESTING CourseType Class\n"
			<< "*********************************************************\n\n";
		CourseType csA150;
		csA150.printCourse();
		csA150.setCourseName("C++ Programming 1");
		csA150.setCourseNumber(150);
		csA150.setCourseUnits(4.0);
		csA150.printCourse();

		CourseType csA250("C++ Programming 2", 250, 4.0);
		cout << csA250.getPrefix() << csA250.getCourseNumber()
			<< " - " << csA250.getCourseName()
			<< " (" << csA250.getCourseUnits() << " units)"
			<< endl;
	}

	{
		cout << "\n\n*********************************************************\n"
			<< "            TESTING Course Class\n"
			<< "*********************************************************\n\n";
		Course csA150;
		csA150.printCourse();
		csA150.setCourseName("C++ Programming 1");
		csA150.setCourseNumber(150);
		csA150.setCourseUnits(4.0);
		csA150.setTransfer('Y');
		int prereqs[] = { 122, 140, 170 };
		csA150.setPrereqs(prereqs, 3);
		csA150.printCourse();
		csA150.printPrereqs();

		vector<int> prerequisites = { 150 };
		Course csA250("C++ Programming 2", 250, 4.0, prerequisites, 'Y');
		cout << csA250.getPrefix() << csA250.getCourseNumber()
			<< " - " << csA250.getCourseName()
			<< " (" << csA250.getCourseUnits() << " units)"
			<< endl;
		csA250.printPrereqs();
		cout << endl;
	}

	cout << endl;
	system("Pause");
	return 0;

}




This is my current output:

**************************************************
            TESTING CourseType CLass
**************************************************

CS A0 - No name assigned (0.00 units)
CS A150 - C++ Programming 1 (4.00 units)
CS A250 - C++ Programming 2 (4.00 units)


**************************************************
            TESTING Course Class
**************************************************

CS A0 - No name assigned (0.00 units, not transferable)
CS A122 or CS A140 or CS A170 or 
CS A150 - CS A150 - C++ Programming 1 (4.00 units, transferable)
CS A150 - Prerequisites:
CS A250 - C++ Programming 2 (4.00 units)
CS A250 - Prerequisites:



And this should be the correct output:

**************************************************
            TESTING CourseType CLass
**************************************************

CS A0 - No name assigned (0.00 units)
CS A150 - C++ Programming 1 (4.00 units)
CS A250 - C++ Programming 2 (4.00 units)


**************************************************
            TESTING Course Class
**************************************************

CS A0 - No name assigned (0.00 units, not transferable)
CS A150 - CS A150 - C++ Programming 1 (4.00 units, transferable)
CS A150 - Prerequisites: CS A122 or CS A140 or CS A170
CS A250 - C++ Programming 2 (4.00 units)
CS A250 - Prerequisites: CS A150




I got the CourseType function print correctly, but I can't get the Course function to print correctly.
Last edited on
bump
You are not getting any help in particular because there is not enough information other than your little snippet of code. More information means more chances you will get help.
By the way, I can tell you one thing :
1
2
3
4
5
6
void Course::printPrereqs() const
{
	int numOfElem;
	cout << getPrefix() << getCourseNumber() << " - "
		<< "Prerequisites: " << endl;  // What value will you be outputting???
}


The sample output requires you to output Prerequisites, but you don't output that field in your code. In other words, this field is missing.
I thought i needed to use the function setPrereqs in order to print out the
CS A122 or CS A140 or CS A170
CS A150
after the "Prerequisites: " in the printPrereqs function?
or was i having the wrong idea??
Last edited on
still unable what to do here. how do i print out the prerequisites.
cause i wouldn't be able to use the setPrereqs function, right?
I am not sure if my function setPrereqs is correct either and I am stuck on printing the correct output
Topic archived. No new replies allowed.