I'm trying to implement a tsp program reading information from a file given by the user in the command line and writing the result in another file which name is also provided in the command line like:
./a.out input.txt output.txt
information from file:
c 1
c 2
c 3
c 4
a 1 2 350
a 1 3 743
a 1 4 982
a 2 3 527
a 2 4 592
a 3 4 719
I am having trouble getting only the numbers from the file. Any help appreciated!
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int x=0, y=0, cMi, city = 0, minCost = 0;
int matrix[18][18], visCit[12], numCit[12];
void mCost(int);
int smallest(int);
int main(int argc, char *argv[])
{
string line;
ifstream file (argv[1]);
while(!(file.eof()))
{
getline(file, line);
for(int i=0; i<line.size(); i++)
{
if( line[i] == 'c' )
{
file.ignore(1, ' ');
numCit[city] = line[i];
city++;
}
else
{
for(int j=0; j<city; j++)
{
for(int k=0; k<city; k++)
{
file.ignore(1, ' ');
matrix[x][y] = line[i][j];
}
visCit[i] = 0;
}
}
}
}
oFile.open(argv[2]);
mCost(0);
oFile.close();
oFile.open(argv[2], ios::out | ios::app);
oFile << "costs " << minCost << endl;
oFile.close();
file.close();
retu 0;
}
void mCost(int cd)
{
int cMi;
visCit[cd] = 1;
oFile << "The tour " << cd+1;
cMi=smallest(cd);
if(cMi == 999)
{
cMi = 0;
oFile << cMi+1 << " ";
minCost+=matrix[cd][cMi];
retu;
}
mCost(cMi);
}
int smallest(int ct)
{
int cit = 999, low = 999, ciMi;
for(int x=0; x<city; x++)
{
if((matrix[ct][x] != 0) && (visCit[x] == 0))
{
if(matrix[ct][i] < low)
{
low=matrix[x][0]+matrix[ct][x];
ciMi=matrix[ct][x];
cit=x;
}
}
}
if(low != 999)
{
minCost+=ciMi;
}
retu cit;
}
