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 print function correctly, but I can't get the Course print function to print correctly.
I need help mostly on the setPrereqs function and the printReqs function in Course class
I guess I am not suppose to use cout in the setPrereqs?
Cause I am just stumped on the setPrereqs and the printPrereqs function in my Course files
bump
Instead of cout in setPrereqs(...) you should feed the member variable prereq with the provided data. Move the entire output statement to printPrereqs().

Tip: You can simplify the output with an if for the " or " in printPrereqs()
1
2
3
4
5
6
7
8
		for (int i = 0; i < prereqs.size(); i++) // Note: prereqs.size()
		{
			if(i > 0) // Note: print 'or' only if there are more than one
				cout << " or "; // This also prevents the wrong 'or' at the end
			cout << getPrefix();
			cout << prereqs[i];
		}
		cout << endl;
Last edited on
okay, the printPrereqs looks better now. Thanks you.
Still having trouble with the setPrereqs, not sure if it's just me, but I can't seem to get it to work.

cause in the Main.cpp, there is a code that reads
csA150.setPrereqs(prereqs, 3);

prereqs being the array and 3 being the number of elements in the array.

and then i have a vector<int> prereq.
I think I am suppose to set the values of the array into the vector
and the it gets printed out normally in the printPrereqs.
The only problem is that I can't seem to get it to work like that

This is currently my output now:

**************************************************
            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 A250 - C++ Programming 2 (4.00 units)
CS A250 - Prerequisites: CS A150


The only part I am missing is the Prerequisites part after it states C++ Programming 1
Okay, so I am not sure if this would be correct, even though i get errors, for my setPrereqs function in my Course.cpp file, but I need to see what i am doing wrong here

1
2
3
4
5
void Course::setPrereqs(const int* prereqs, int numOfElem) const
{
	for (int i = 0; i <= numOfElem; i++)
		this->prereq.at(i) = prereqs[i];
}



And the revised version of printPrereqs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Course::printPrereqs() const
{
	cout << getPrefix() << getCourseNumber() << " - "
		<< "Prerequisites: " << getPrefix();
	for (int i = 0; i < prereq.size(); i++)
	{
		if (i > 0)
			cout << " or ";

		cout << this->prereq.at(i);
	}
	cout << endl;

}
Last edited on
Can someone explain to me what is wrong with my setPrereqs function?
I am lost as to how to fix it
That's because you call print before setting coursename,etc. For coursea50 which are constructed with the default constructor
solved

I needed to use the push_back function for my setPrereqs so it would have looked like this:
1
2
3
4
5
void Course::setPrereqs(const int* prereqs, int numOfElem)
{
	for (int i = 0; i <= numOfElem; i++)
		this->prereq.push_back(prereqs[i]);
}
I needed to use the push_back function for my setPrereqs so it would have looked like this:
This might be wrong depending on what you want to achieve. I would expect that setPrereqs(...) replaces the existing data. This would be done like this:
1
2
3
4
void Course::setPrereqs(const int* prereqs, int numOfElem)
{
	this->prereq.assign(prereqs, prereqs + numOfElem);
}
http://www.cplusplus.com/reference/vector/vector/assign/

If you in fact want to add the data you might consider to rename the function accordingly like addPrereqs(...).
Topic archived. No new replies allowed.