Ostream To String Help

Why is cout giving an error, cannot convert ostream to string? And if that's the problem does anyone know a way to do that?

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include<vector>
#include<fstream>
#include<ostream>
#include<iomanip>
using namespace std;

struct Vertex {
	int home, away, wt;
};

typedef pair<int, int> conn;

class Graph
{
public:
	vector<vector<conn> > adjList;

	Graph(vector<Vertex> const &vert, int N) {
		//N will be 50 here
		adjList.resize(N);

		for (auto &x : vert)
		{
			int home = x.home;
			int away = x.away;
			int wt = x.wt;

			//This is like inserting node to the end of the list.
			adjList[home].push_back(make_pair(away, wt));

			//since this is an undirected Graph
			//adjList[away].push_back(make_pair(home, wt));

		}
	}
};



void printSpecific(Graph const &graph, string a[], int user) {
	std::ostringstream os;
	os << "Hello world";
	std::string s = os.str();
	const char *p = s.c_str();
	cout << a[user - 1] << ": ";
	for (conn x : graph.adjList[user]) {
		cout << a[x.first - 1] << ", ";
	}
}

void displayList(Graph const &graph, string a[]) {
	for (int i = 1; i <= 50; i++) {
		//cout<<setw(10);
		cout << i << ". " << a[i - 1] << "   ";
		if (i % 2 == 0) {

			cout << "\n";
		}
	}
}
void printGraph(Graph const &graph, int N, string c[])
{
	//print out the list of a specific Vertex
	for (int i = 1; i < N; i++)
	{
		cout << c[i - 1] << "-";
		for (conn a : graph.adjList[i])
		{
			//cout<<a.first<<endl;
			cout << c[a.first - 1] << "  ";
			// cout<<"("<<i<<", "<<a.first<<", "<<a.second<<") ";
		}
		cout << endl;
	}
}

vector<Vertex> readData()
{
	fstream file;
	vector<Vertex> v(0);

	file.open("country.txt");
	int i = 0, x, y, z;
	while (file >> x >> y >> z) {
		//cout<<x<<"  "<<y<<"  "<<z<<endl;
		v.push_back({ x, y,z });

	}
	file.close();

	//this should return the vector container
	return v;
}
int main() {
	/*
	1 -Albania
	2 -Andorra
	3 -Armenia
	4 -Austria
	5 -Azerbaijan
	6 -Belarus
	7 -Belgium
	8 -Bosnia and Herzegovina
	9 -Bulgaria
	10-Croatia
	11-Cyprus
	12-Czech Republic
	13-Denmark
	14-Estonia
	15-Finland
	16-France
	17-Georgia
	18-Germany
	19-Greece
	20-Hungary
	21-Iceland
	22-Ireland
	23-Italy
	24-Kazakhstan
	25-Latvia
	26-Liechtenstein
	27-Lithuania
	28-Luxembourg
	29-Macedonia (FYROM)
	30-Malta
	31-Moldova
	32-Monaco
	33-Montenegro
	34-Netherlands
	35-Norway
	36-Poland
	37-Portugal
	38-Romania
	39-Russia
	40-San Marino
	41-Serbia
	42-Slovakia
	43-Slovenia
	44-Spain
	45-Sweden
	46-Switzerland
	47-Turkey
	48-Ukraine
	49--United Kingdom (UK)
	50-Vatican City (Holy See)
	*/
	string country[50] = { "Albania", "Andorra", "Armenia","Austria","Azerbaijan","Belarus","Belgium","Bosnia and Herzegovina","Bulgaria","Croatia","Cyprus",
		"Czech Republic","Denmark","Estonia","Finland","France","Georgia","Germany","Greece","Hungary","Iceland","Ireland","Italy","Kazakhstan","Latvia",
		"Liechtenstein","Lithuania","Luxembourg","Macedonia","Malta","Moldova","Monaco","Montenegro","Netherlands","Norway","Poland","Portugal","Romania",
		"Russia","San Marino","Serbia","Slovakia","Slovenia","Spain","Sweden","Switzerland","Turkey","Ukraine","United Kingdom","Vatican City" };

	int n = 51, cchoice, cho;
	bool end = true;
	vector<Vertex> vert = readData();
	Graph graph(vert, n);
	displayList(graph, country);
	while (end) {
		cout << "\n1. Bordering countries of country number(select from the list above)\n";
		cout << "2. Bordering countries of all the European nations\n";
		cout << "3 ";
		cin >> cho;
		switch (cho) {
		case 1: cout << "\nBordering nations of the chosen nation.\n";
			cout << "\nShow me the bordering nations of (number): " << endl;
			cin >> cchoice;
			printSpecific(graph, country, cchoice);
			break;

		case 2: cout << "All European countries with their borders: \n";
			printGraph(graph, n, country);
			break;

		case 3: cout << "Exiting the program\n" << endl;
			end = false;
			break;

		default: cout << "Wrong Choice, please try again." << endl;
		}
	}


	return 0;
}
Last edited on
I don't get any errors when I run it on here. Just two warnings about unused variables. (lines 45 and 84).
You are missing the includes of two required headers:
#include <string> // to use the full functionality of std::string
#include <sstream> // to use the full functionality of string streams


Note: it is not necessary to explicitly #include <ostream>, the header <iostream> includes it.
Topic archived. No new replies allowed.