TSP read input file problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 5
Reputation: sycc2172 is an unknown quantity at this point 
Solved Threads: 0
sycc2172 sycc2172 is offline Offline
Newbie Poster

TSP read input file problem

 
0
  #1
Sep 16th, 2007
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

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

  1. #include <cstdlib> //to use exit()
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. const int ARRAY_SIZE = 100;
  7.  
  8. void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE]);
  9.  
  10. int main()
  11. {
  12. //Declaring array
  13. int connection [ARRAY_SIZE];
  14. int connection1 [ARRAY_SIZE];
  15.  
  16. getConnectionValue(connection, connection1);
  17. return 0;
  18. }
  19.  
  20. void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE])
  21. {
  22. //Declare stream and open the first .txt file
  23. ifstream inStream;
  24. inStream.open("Input1.txt");
  25.  
  26. if (inStream.fail())
  27. {
  28. cerr << "Can't open input file!\n" << endl;
  29. exit(1);
  30. }
  31.  
  32. int i=0;
  33. while (!inStream.eof())
  34. {
  35. inStream >> connection[i] >> connection1[i];
  36.  
  37. cout << i+1 << "numbers " << connection[i] << "and "<< connection1[i] << "\n" << endl;
  38. i++;
  39. }
  40. inStream.close();
  41.  
  42. }
Last edited by sycc2172; Sep 16th, 2007 at 12:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: TSP read input file problem

 
0
  #2
Sep 16th, 2007
Is the text file in the same directory as your program?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: sycc2172 is an unknown quantity at this point 
Solved Threads: 0
sycc2172 sycc2172 is offline Offline
Newbie Poster

Re: TSP read input file problem

 
0
  #3
Sep 16th, 2007
Yes it is.. I put it into the Debug folder. The file name is exactly the same
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: TSP read input file problem

 
1
  #4
Sep 16th, 2007
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?
Last edited by iamthwee; Sep 16th, 2007 at 12:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: sycc2172 is an unknown quantity at this point 
Solved Threads: 0
sycc2172 sycc2172 is offline Offline
Newbie Poster

Re: TSP read input file problem

 
0
  #5
Sep 16th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: TSP read input file problem

 
0
  #6
Sep 16th, 2007
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.
Last edited by iamthwee; Sep 16th, 2007 at 12:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: sycc2172 is an unknown quantity at this point 
Solved Threads: 0
sycc2172 sycc2172 is offline Offline
Newbie Poster

Re: TSP read input file problem

 
0
  #7
Sep 16th, 2007
Yes please. I'm just a new student taking C++ lesson this semester. Any advice would be much appreciated
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: TSP read input file problem

 
0
  #8
Sep 16th, 2007
Oops sorrie I gotta go.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 5
Reputation: sycc2172 is an unknown quantity at this point 
Solved Threads: 0
sycc2172 sycc2172 is offline Offline
Newbie Poster

Re: TSP read input file problem

 
0
  #9
Sep 16th, 2007
Okay.. nvm.. Thx for the help. I'll proceed with the code...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: TSP read input file problem

 
0
  #10
Sep 16th, 2007
Yeah basically you wanna use something like

  1. #include <iostream> //basic i/o
  2. #include <fstream> //file input out
  3. #include <string> //string handling
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. //open a file for reading
  10. ifstream read ("foo.txt");
  11. string chunk; //this will be the string which holds each word or number
  12.  
  13. while ( read >> chunk )
  14. {
  15. cout << chunk << endl;
  16. }
  17.  
  18. read.close(); //close file
  19. return 0;
  20. }
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.
Last edited by iamthwee; Sep 16th, 2007 at 4:39 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC