How do I sort the index of "cities[I]" which are the latitudes and longitudes without changing all the elements associated with the that "city[I]" element?
In another word, how do I sort the array line of latitudes and longitudes without changing countries and city it is associated with?
I don't really want to use struct is I do not have to, I need to sort latitudes and longitudes of a city that is picked from a cvs file from the user and sort the city's lat and lon from high to low or vice versa to the city the user pick.
but by sorting it high to slow, how to I do it and keep cities and countries along with their lat and long while sorting?
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#define _USE_MATH_DEFINES
#include <math.h>
usingnamespace std;
int main()
{
//input user for a name on city list
string input;
do
{
cout << "The worldcities.cvs contained infor on 120 cities." << endl;
cout << "Enter a city by name or number (1-120)" << endl;
std::getline (std::cin,input);
constint MAX_ARRAY_SIZE = 121;
string cities[MAX_ARRAY_SIZE];
string countries[MAX_ARRAY_SIZE];
string latitudes[MAX_ARRAY_SIZE];
string longitudes[MAX_ARRAY_SIZE];
string temp;
ifstream file("worldcities.csv");
if(!file.is_open())
{
// file couldn't be opened
cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
cin.get();
return 0;
getline(file, temp);
}
//inputs the file into 4 arrays for each catergories
for (int i=0;getline(file,(cities[i]),',');i++)
{
getline(file, countries[i], ',');
getline(file, latitudes[i], ',') ;
getline(file, longitudes[i]);
if(input == cities[i])
{
cout << "The city selected is" << " " << cities[i] << latitudes[i] <<endl;
}
}
if(false)
{
cout << "I don't understand your input" << endl;
continue;
}
}while (true);
system("pause");
return 0;
}
Easy way: use structs and let standard library do their work for you.
Hard way: implement your own sorting algorithm and each time you swap values in one array, swap corresponding values in other arrays.