Using 2 vectors to output on the same line...

Ok so im working on a problem that I want to output:

"Mega City" is 0.0 miles from Imperial City.
"Spring City" is 44.9 miles from Imperial City.
"Affirmation City" is 100.5 miles from Imperial City.
"Outskirt City" is 142.1 miles from Imperial City.

The sum of all distances is 287.5 miles.


I already know how to do sum I need to know what I'm doing wrong in my code.
Here it is:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
vector <double> vdistance(4);
vdistance [0] = 0.0;
vdistance [1] = 44.9;
vdistance [2] = 100.5;
vdistance [3] = 142.1;

vector <string> vcity(4);
vcity [0] = "Imperial City";
vcity [1] = "Spring City";
vcity [2] = "Affirmation City";
vcity [3] = "Outskirt City";


double sum = 0;

for(int c = 0; c < vcity.size(); ++c)
cout << "\"" << vcity[c] << "\" is ";

for(int i = 0; i < vdistance.size(); ++i)
cout << vdistance[i] << " miles from Imperial City\n";

// This gets the total sum of all distances
for(int j = 0; j < vdistance.size(); ++j)
sum += vdistance[j];
cout << "\nThe sum of all distances is " << sum << " miles.\n";

return 0;
}

@Beta4

You were very close.
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
// 2 Vectors.cpp : main project file.

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;
int main()

{
	vector <double> vdistance(4);
	vdistance [0] = 0.0;
	vdistance [1] = 44.9;
	vdistance [2] = 100.5;
	vdistance [3] = 142.1;

	vector <string> vcity(4);
	vcity [0] = "Imperial City";
	vcity [1] = "Spring City";
	vcity [2] = "Affirmation City";
	vcity [3] = "Outskirt City";


	double sum = 0;

	for(int c = 0; c < vcity.size(); c++)
	{
		cout << "\"" << vcity[c] << "\" is ";

	//for(int i = 0; i < vdistance.size(); ++i)  // Not needed
		cout << vdistance[c] << " miles from Imperial City\n"; // Now prints city and distance on one line
               // Changed the i loop, to be in the c loop.
	}

	// This gets the total sum of all distances
	for(int j = 0; j < vdistance.size(); ++j)
		sum += vdistance[j];
	cout << "\nThe sum of all distances is " << sum << " miles.\n";

	return 0;
}
Great thanks a bunch... knew I was close lol.
Topic archived. No new replies allowed.