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:
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";
// 2 Vectors.cpp : main project file.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
usingnamespace 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;
}