helloz

I am trying to make a program that calculates the distance between several cities and prints out the shortest distance of them all, the input will come from a textfile e.g
3 3 A 2 1 B 1 2 C 2 2 where the first two numbers are row and columns of the array respectively, A is the name of the city and the two numbers after it are the x and y co-ordinates, I have read in the data I am having trouble looping through the data.
Here is what I have done so far:

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cmath>
#include <vector>

using namespace std;

int main(int argc,char* argv[])
{
 int ycord=0;
 int xcord=0;
 int rows=0;
 int cols=0; 
 int** array;

vector<string> cities;
 array = new int*[rows];
 string str="";
ifstream input(argv[1]);
ofstream output(argv[2]);
    
    input>>rows;
    input>>cols;
    int num=cols*2;
      
     array[0]=new int[num];

      for(int j=0; j<num; j++)
        {
          array[0][j]=0; 
        }

    /*while(input >> str >> xcord >> ycord)
      {
       
      }*/ 
  

      for(int i=0; i<num;i+=2)
        {
         input>>str;
         cities.push_back(str);
          
         input>>xcord;
         array[0][i]=xcord;
         input >> ycord;
         array[0][i+1]=ycord; 
        }
      
  for(int j=0; j<num; j++)
  cout<<array[0][j]<<endl;
  for(int i=0; i<cities.size();i++)
   cout<<"Cities: "<<cities.at(i)<<endl;
input.close();
output.close();



return 0;   
}

I store the co-ordinates in a 2d array with 1 row and 6 columns

Trying to do that with multiple arrays is not a good idea because its difficult to keep the data together for any given city. A more convenient way to do it is to create a structure for the data

struct city
{
   std::string name;
   int x;
   int y;
};

Now just make a vector of the above structure. It completely eliminates the need to use the new operator to allocate memory for anything.

vector<city> cities;
city c;
while( input >> c.name >> c.x >> c.y )
{
   cities.push_back(c);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.