944,061 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4024
  • C++ RSS
Sep 16th, 2007
0

TSP read input file problem

Expand Post »
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

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sycc2172 is offline Offline
5 posts
since Sep 2007
Sep 16th, 2007
0

Re: TSP read input file problem

Is the text file in the same directory as your program?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 16th, 2007
0

Re: TSP read input file problem

Yes it is.. I put it into the Debug folder. The file name is exactly the same
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sycc2172 is offline Offline
5 posts
since Sep 2007
Sep 16th, 2007
1

Re: TSP read input file problem

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 16th, 2007
0

Re: TSP read input file problem

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sycc2172 is offline Offline
5 posts
since Sep 2007
Sep 16th, 2007
0

Re: TSP read input file problem

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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 16th, 2007
0

Re: TSP read input file problem

Yes please. I'm just a new student taking C++ lesson this semester. Any advice would be much appreciated
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sycc2172 is offline Offline
5 posts
since Sep 2007
Sep 16th, 2007
0

Re: TSP read input file problem

Oops sorrie I gotta go.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 16th, 2007
0

Re: TSP read input file problem

Okay.. nvm.. Thx for the help. I'll proceed with the code...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sycc2172 is offline Offline
5 posts
since Sep 2007
Sep 16th, 2007
0

Re: TSP read input file problem

Yeah basically you wanna use something like

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with windows api in c++
Next Thread in C++ Forum Timeline: C++ Help Immediately!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC