inserting vector

I need to attach CourseTitle at the end of each CourseCode. I'm trying to use "insert" but I'm not getting it right so far. I googled around and it seems like I have to use what is called an iterator.

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

// Simple code to store courses using vectors and strings

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <iomanip>


using namespace std;

int main(void)
{
	const string degreeCode("PHYS");
	
	// Gather list of courses and their codes from user,
	// storing data as a vector of strings
	vector<string> VectorOne;
	vector<string> VectorTwo;
	char CourseCode[50];
	char CourseTitle[50]; 
	bool ExitLoop;
	int i=0; 
	
	
	ExitLoop = false;
	
	do
	{
		cout << "Enter course code (enter 'done' to exit): ";
		cin.getline(CourseCode, 50);
		
		if (strcmp(CourseCode,"done") == 0)
		{
			ExitLoop = true;
		}
		
		else
		{
			cout << "Enter course title (enter done to exit): ";
			cin.getline(CourseTitle, 50); 
			VectorOne.push_back(CourseCode);
			VectorTwo.push_back(CourseTitle);
			i=i+1;
		}
	}
	while(!ExitLoop);
	
	vector <string> FullVector;
	FullVector.reserve(VectorOne.size() + VectorTwo.size()); 
	FullVector.insert(VectorOne.end(), VectorOne.begin(), VectorOne.end()); 
	FullVector.insert(VectorTwo.end(), VectorTwo.begin(), VectorTwo.end());
	
	for(int j=0;j<i;j++)
	{
		cout<<FullVector[i];
	}

	

	getc(stdin);
	
	
	
	return 0;
}
Last edited on
The following is the criteria for my program. Am I on the right track so far?

Write a C++ program to store and print physics courses
-Allow the user to enter an arbitrary number of courses
- Ask the user to separately provide a course code (integer, e.g. 30762) and title (string, e.g. Programming in C++)
- Use a string stream to create a string containing the full course title, e.g. PHYS 30762 Programming in C++
- Each of these strings should be stored in a vector
- Print out full course list using an iteratorAssignment 3: physics course database
- Your program should correctly use
- a vector of strings to store course data (1 mark)
- a string stream to combine course code and title (1 mark)
- an iterator to print out information for each course (1 mark)
- Challenge mark: your code should also be able to print out a list of courses for a particular year, as identi ed by the first digit of the course code
Topic archived. No new replies allowed.