Greetings.

I'm currently doing a project on Traveling Salesman Problem. But I'm not asking for answers for the whole project. I just started my programming lessons so I hope can get some help from all of you.

About this project: Initially you have to read 2 input files, Input1.txt and Input2.txt. An example of the contents for these 2 input files are as follows:

Input1.txt:
0 1
0 7
1 0
1 2 and so on..

Input2.txt:(Showing location name, first 3 numbers are for longitude and the next 3 numbers for latitude. Units for longitude are deg, min, sec)
Africa 111 85 23 2 14 59.72
America 112 27 55 2 29 35.29
China 101 21 22.31 2 20 21.29 and so on.....

My problem right now is after I wrote the codes to read the first file as shown below, it always display "Can't Open Input File!" as its cout result. Can I know where is it gone wrong? Another question is since I'm using array to store all the data, is using pointer will be easier than using array? Thank you for your help

-----------------------------------------------------------------------------------------------

#include <cstdlib> //to use exit()
#include <iostream>
#include <fstream>
using namespace std;

const int ARRAY_SIZE = 100;

void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE]);

int main()
{
	//Declaring array
	int connection [ARRAY_SIZE];
	int connection1 [ARRAY_SIZE];

	getConnectionValue(connection, connection1);
	return 0;
}

void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE])
{
	//Declare stream and open the first .txt file
	ifstream inStream;
	inStream.open("Input1.txt"); 
	
	if (inStream.fail()) 
	{
		cerr << "Can't open input file!\n" << endl;
		exit(1);		
	}
	
	int i=0;
	while (!inStream.eof())
	{	
		inStream >> connection[i] >> connection1[i];
			
		cout << i+1 << "numbers " << connection[i] << "and "<< connection1[i] << "\n" << endl;
		i++;
	}
	inStream.close();
		
}

Recommended Answers

All 9 Replies

Member Avatar for iamthwee

Is the text file in the same directory as your program?

Yes it is.. I put it into the Debug folder. The file name is exactly the same

Member Avatar for iamthwee

Can you try dumping your text file in the c: drive then change your program so this line:inStream.open("Sarawak_Cnx.txt");

becomes: inStream.open("C:\\Sarawak_Cnx.txt");

What happens now?

commented: <.< +3

Argh... I got it! I put into the wrong directory. Thanks for the advice.

One question. Is the code above able to change to using pointer instead of arrays?

Member Avatar for iamthwee

Um no using an array and indexing should be easier to understand than pointers and is perfectly ok. I'd stick with that.

Just to let you know the way you are reading in data from a file is wrong and horribly flawed. Shall I show you the correct way.

Yes please. I'm just a new student taking C++ lesson this semester. Any advice would be much appreciated

Member Avatar for iamthwee

Oops sorrie I gotta go. :(

Okay.. nvm.. Thx for the help. :) I'll proceed with the code...

Member Avatar for iamthwee

Yeah basically you wanna use something like

#include <iostream> //basic i/o
#include <fstream> //file input out
#include <string> //string handling

using namespace std;

int main()
{
  //open a file for reading
  ifstream read ("foo.txt");
  string chunk; //this will be the string which holds each word or number

  while ( read >> chunk )
  {
   cout << chunk << endl;
  }
  
  read.close(); //close file
  return 0;
}

to read files, instead of eof as that has problems. As a newbie I don't think it is important to bore you with the details as to why eof is bad.

Obviously after that you need to convert the string to actual numbers. But I'm not sure if you are allowed to use strings. Let me know.

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.