filestream && multidimensional arrays

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

Join Date: Oct 2005
Posts: 4
Reputation: aikawa is an unknown quantity at this point 
Solved Threads: 0
aikawa aikawa is offline Offline
Newbie Poster

filestream && multidimensional arrays

 
0
  #1
Oct 26th, 2005
Cheers guys, my first post here. I've been reading a lot but i can't seem to find any scenario that helps me out with my problem. I'm trying to take input from a file in the form of some shape... rectangle/square, and store it somehow. The shape is composed of letters and represents a map. Also I'm going to be using an (x,y) coordinate system later on so i have figured out that using a multidimensional array is imperative. I've seemed to have pseudo coded this well, but I can't seem to find to convert from paper to keyboard reading the file into a multidimensional array. So far i know i need the getline function... but i don't quite understand how i'm going to fill the multidimensional array with a getline function.

If someone could give me an example or nudge me in the right direction that would be awesome. Thanks!

btw the input file looks soemthing like... where x is a wall and o is walking area

xxxxxxxxxxxxxxxx
xoooxxxxxxxxoox
xoooxxxxoooooxx
xoooxxoooooxxxx
xoooxxxxooooooo
xxxxooooooxxxxx
xxxxxxoooooxxxx
xxxxxxxxxxxoxxxx
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: filestream && multidimensional arrays

 
0
  #2
Oct 26th, 2005
To store that type of information in an array I would use a 2D array of char. I would read in one char at a time, using a nested for loop where the outer loop controlled which row I was in and the inner loop controlled which column I was in. Then I could explicitly access any element within the array by using syntax such as myArrayofChar[row][col]. Remember that the indexes of an array are zero based, so the first row has index 0 and the 5 column has index 4, not five. If I knew ahead of time the number of rows and columns I'd declare the array using syntax, assuming it wasn't huge in size. Otherwise, I'd declare the array on the heap using dynamic memory with the keywords new[] and delete[] and loops as befitting the mulidimensional characterstics of the array.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: aikawa is an unknown quantity at this point 
Solved Threads: 0
aikawa aikawa is offline Offline
Newbie Poster

Re: filestream && multidimensional arrays

 
0
  #3
Oct 27th, 2005
Thanks Lerner for the reply! ... So i got that much now i have this really odd problem. I think when i count my columns and rows in the file it's not resetting when i actually want to read in the letters from the file. I've tried opening and closing and .clear()... none of these things help out

  1.  
  2. class cave
  3. {
  4. public:
  5. cave();
  6. void cavemap();
  7. void set_numrows();
  8. void make_move();
  9.  
  10. private:
  11. ifstream infile;
  12. char letterz;
  13. int numrows; // This is the number of rows
  14. int numcols; // This is the number of columns
  15. char **multArray;
  16. };
  17.  
  18. void cave::cavemap()
  19. {
  20. infile.open("cavemap.txt");
  21.  
  22. //Block of code to find number of columns
  23. ////////////////////////////
  24. string str;
  25. getline(infile, str);
  26. numcols=str.length();
  27. infile.close();
  28. ////////////////////////////
  29.  
  30. infile.open("cavemap.txt");
  31. while(!infile.eof()) //while-loop to count rows
  32. {
  33. string temp;
  34. getline(infile, temp);
  35. numrows++;
  36. }
  37.  
  38. multArray = new char * [numrows]; // allocate the rows
  39.  
  40. for (int row = 0; row < numrows; row++) //allocate columns
  41. multArray[row] = new char [numcols];
  42.  
  43. for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array
  44. {
  45. for (int c = 0; c < numcols; c++)
  46. {
  47. infile>>letterz;
  48. multArray[r][c] = letterz;
  49. cout << setw(2) << multArray[r][c];
  50. }
  51. cout << endl;
  52. }
  53. for (int row2 = 0; row2 < numrows; row2++)
  54. delete[] multArray[row2]; // destroys memory of a single row
  55.  
  56. delete[] multArray; // destroys the pointers to each row
  57. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: filestream && multidimensional arrays

 
0
  #4
Oct 27th, 2005
In the code you've posted I don't see where you initialize numrows to a value before you call the postfix increment operator on it. That would be a no-no.

For some other comments, see below.
  1. //Block of code to find number of columns
  2. ////////////////////////////
  3. string str;
  4.  
  5. //numrows should have a value of zero here
  6.  
  7. getline(infile, str);
  8.  
  9. //you somehow need to give numrows the value of one here
  10.  
  11. numcols=str.length();
  12. infile.close();
  13. ////////////////////////////
  14.  
  15. //infile is already open here, don't reopen it.
  16. infile.open("cavemap.txt");
  17.  
  18. //don't use eof() as the terminating condition for the while loop. If you want to know the technical reason why, ask. Otherwise just accept the advice. Use while(getline(infile, str)) instead and don't call getline() within the loop body.
  19. while(!infile.eof()) //while-loop to count rows
  20. {
  21. //you really don't need another variable, just keep using str
  22. string temp;
  23. getline(infile, temp);
  24. numrows++;
  25. }
  26.  
  27. /*This loop will stop when EOF is found or something else causes infile to go into a
  28. failed state. To be sure all of file was read you can check .eof(). If it is true, then you know you successfully read the whole file.
  29.  
  30. However, you want to reread the file now char by char. To do that you need
  31. to get back to the beginning of the file. To do that I would call the istream clear() method on infile (because finding EOF put infile into a failed state) and then I would either use the istream seekg() method with the appropriate parameters or close infile and reopen it (when you reopen it it should default to the beginning of the file you associate it with). Now you can reread the file char by char to fill in multarray using the nested loop.
  32. */
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: aikawa is an unknown quantity at this point 
Solved Threads: 0
aikawa aikawa is offline Offline
Newbie Poster

Re: filestream && multidimensional arrays

 
0
  #5
Oct 27th, 2005
ohhh well i guess i shoulda included the whole program


  1. #include <iostream> // for cout
  2. #include <iomanip> // for setw
  3. #include <fstream>
  4. #include <string>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8.  
  9. class cave
  10. {
  11. public:
  12. void cavemap();
  13. void set_numrows();
  14. void make_move();
  15.  
  16. private:
  17. ifstream infile;
  18. char letterz;
  19. int numrows; // This is the number of rows
  20. int numcols; // This is the number of columns
  21. char **multArray;
  22. };
  23.  
  24. void cave::cavemap()
  25. {
  26. infile.open("cavemap.txt");
  27.  
  28. //Block of code to find number of columns
  29. ////////////////////////////
  30. string str; //
  31. getline(infile, str); //
  32. numcols=str.length(); //
  33. ////////////////////////////
  34.  
  35. while(!infile.eof()) //while not end of file get each line and increase the number of rows by 1.
  36. {
  37. string temp;
  38. getline(infile, temp);
  39. numrows++;
  40. }
  41.  
  42. multArray = new char * [numrows]; // allocate the rows
  43.  
  44. for (int row = 0; row < numrows; row++) // now allocate the columns
  45. multArray[row] = new char [numcols];
  46.  
  47. for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array
  48. {
  49. for (int c = 0; c < numcols; c++)
  50. { // :)
  51. infile>>letterz;
  52. multArray[r][c] = letterz;
  53. cout << setw(2) << multArray[r][c];
  54. }
  55. cout << endl;
  56. }
  57. cin>>letterz;
  58. for (int row2 = 0; row < numrows; row++)
  59. delete[] multArray[row2]; // destroys memory of a single row
  60.  
  61. delete[] multArray; // destroys the pointers to each row
  62. }
  63.  
  64. void cave::set_numrows()
  65. {
  66. numrows = 1;
  67. }
  68.  
  69. void cave::make_move()
  70. {
  71.  
  72.  
  73. }
  74.  
  75.  
  76. int main ()
  77. {
  78. cave caveobj;
  79.  
  80. caveobj.cavemap();
  81.  
  82. return 0;
  83. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: filestream && multidimensional arrays

 
0
  #6
Oct 28th, 2005
In main() you have the following two lines:

cave caveobj;
caveobj.cavemap();

The first line calls the default constructor for the cave class. Since you don't explicitly declare/define a default constructor the compiler does it for you. The default default constructor will not initialize any data members for you. So numrows is left uninitialized at this time.

The second line calls the cavemap() method on the object called caveobj which is an instance of the cave class. However, numrows isn't initialized in that method either because there is no explicit initialization/assignment of a value before the postscript operator is called and there is no call to set_numrows() from within cavemap() that would set numrows to 1. Either use an initializer list in your own default constructor to set the value of numrows to the desired default value, OR explicitly assign a default value to numrows within cavemap() before you call the postscript operator, OR call set_numrows() within cavemap() before you call the postscript operator on numrows.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: aikawa is an unknown quantity at this point 
Solved Threads: 0
aikawa aikawa is offline Offline
Newbie Poster

Re: filestream && multidimensional arrays

 
0
  #7
Oct 28th, 2005
I see... I've made some revisions since then.. but my map still doesn't display correctly :mad: I have a mutator function set_rows that sets numrows to one. Then i call my cave_map() member function to populate my 2d array. Next i have a function called make_move(), which checks all the places around the current position using (x, y) coordinates and after moving sets the last place to '*'. I do this because i have to have a way to backtrack, so eventually i'll need a way to store these places in an array so if i come to a dead end on the map i can backtrack to the last place. Only problem is I don't know how far i would backtrack. I'm thinking maybe start a count sequence after a path is chosen if there is two choics... if there's a dead end back track until the counter is 0.

  1.  
  2. #include <iostream> // for cout
  3. #include <iomanip> // for setw
  4. #include <fstream>
  5. #include <string>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. class cave
  11. {
  12. public:
  13. cave();
  14. void cavemap();
  15. void set_numrows();
  16. void make_move();
  17. void set_letterz();
  18. void map_output();
  19.  
  20. private:
  21. ifstream infile;
  22. char letterz;
  23. int numrows; // This is the number of rows
  24. int numcols; // This is the number of columns
  25. char **multArray; // Note: We use int ** because we want multArray to be a
  26. // a pointer to pointer to a int (an array of arrays...)
  27. int x, y;
  28. };
  29.  
  30. cave::cave()
  31. {
  32. x = 0;
  33. y = 0;
  34. }
  35. void cave::set_letterz()
  36. {
  37. letterz=' ';
  38. }
  39.  
  40. void cave::cavemap()
  41. {
  42. infile.open("cavemap.txt");
  43. if(infile.fail())
  44. {
  45. cout<<"File opening fail"<<endl;
  46. }
  47. //Block of code to find number of columns
  48. ///////////////////////////////////////////////////////////////
  49. string str; //
  50. getline(infile, str); //
  51. numcols=str.length(); //
  52. //
  53. //
  54. while(getline(infile, str)) //while-loopo to count rows //
  55. { //
  56. numrows++; //
  57. } //
  58. infile.clear(); //
  59. ////////////////////////////////////////////////////////////////
  60.  
  61.  
  62. multArray = new char * [numrows]; // allocate the rows
  63.  
  64.  
  65. for (int row = 0; row < numrows; row++) // now allocate the columns
  66. multArray[row] = new char [numcols];
  67.  
  68. for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array
  69. {
  70. for (int c = 0; c < numcols; c++)
  71. {
  72. infile>>letterz;
  73. multArray[r][c] = letterz;
  74. cout << setw(2) << multArray[r][c];
  75. }
  76. cout << endl;
  77. infile.close();
  78. }
  79. for (int row2 = 0; row2 < numrows; row2++)
  80. delete[] multArray[row2]; // destroys memory of a single row
  81.  
  82. delete[] multArray; // destroys the pointers to each row
  83. }
  84.  
  85. void cave::set_numrows()
  86. {
  87. numrows = 1;
  88. }
  89.  
  90. void cave::make_move() //sequence of if-else statements checks all 8 directions for "o" and then sets the previous
  91. //"y" = "o" and the next "o" equal to "y"
  92. {
  93. if(multArray[x+1][y+1]='o') //Check Northeast
  94. {
  95. multArray[x][y]='*';
  96. multArray[x+1][y+1]='y';
  97. }
  98. else if(multArray[x+1][y]='o')//Check East
  99. {
  100. multArray[x][y]='*';
  101. multArray[x+1][y]='y';
  102. }
  103. else if(multArray[x+1][y-1]='o') //check Southeast
  104. {
  105. multArray[x][y]='*';
  106. multArray[x+1][y-1]='y';
  107. }
  108. else if(multArray[x][y-1]='o') //Check South
  109. {
  110. multArray[x][y]='*';
  111. multArray[x][y-1]='y';
  112. }
  113. else if(multArray[x-1][y-1]='o')//check Southwest
  114. {
  115. multArray[x][y]='*';
  116. multArray[x-1][y-1]='y';
  117. }
  118. else if(multArray[x-1][y]='o') //Checks west
  119. {
  120. multArray[x][y]='*';
  121. multArray[x+1][y]='y';
  122. }
  123. else if(multArray[x-1][y+1]='o') //Checks NorthWest
  124. {
  125. multArray[x][y]='*';
  126. multArray[x-1][y+1]='y';
  127. }
  128. else if(multArray[x][y+1]='o') //Checks North
  129. {
  130. multArray[x][y]='*';
  131. multArray[x+1][y]='y';
  132. }
  133. }
  134. void cave::map_output()
  135. {
  136.  
  137. for (int r = 0; r < numrows; r++) //double for-loop to populate the 2D array
  138. {
  139. for (int c = 0; c < numcols; c++)
  140. {
  141. cout << setw(2) << multArray[r][c];
  142. }
  143. cout << endl;
  144.  
  145. }
  146. }
  147.  
  148.  
  149.  
  150. int main ()
  151. {
  152.  
  153. cave caveobj;
  154. caveobj.set_numrows();
  155. caveobj.cave_map();
  156. caveobj.make_move();
  157. caveobj.map_output();
  158.  
  159.  
  160.  
  161. return 0;
  162. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: filestream && multidimensional arrays

 
0
  #8
Oct 30th, 2005
When you clear() the stream it doesn't reposition the pointer to the beginning of the file. You can do that by using seekg() with the appropriate parameters, or by closing and reopening the same file.

Then, don't destroy the memory of the cave before you actually search the maze! Destroy the memory after you've found a solution, prove there is no solution, and do something with that knowledge. As it is now, when you successfully load the maze from the file to your program you won't be able to do anything with make_move(), because the maze will be gone already before you call make_move().


Maze searching can be a maze itself. The idea of using an array of neighbors and somehow keeping track of which ones have already been visited sounds logical. Then one approach would be to consider having a dequeue of cells representing the successfully visited cells (the "path" or "solution"). Each successfully visited cell would be pushed on the back of the dequeue. If a dead end was found then pop the current cell from the end stack to go "back" to the next available cell. In this scenario, stop searching if "goal" is found or if the dequeue is empty. IF "goal" found, then print the dequeue from begining to end to print a given solution. IF the dequeue was empty, then the maze has no solution. The number of steps from start to goal would be the size of the dequeue if you wanted to search the maze again to see if there was another, or shorter, solution.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: ankitnigam is an unknown quantity at this point 
Solved Threads: 0
ankitnigam ankitnigam is offline Offline
Newbie Poster
 
0
  #9
25 Days Ago
Write a function for seat allocate & seat reserved.Seat allocate array and seat reserver array.Seat allocate array is of 10*20 & each row & column represent A1,A2....;B1,B2.....;J1,J2 & so on i.e row are A to J whereas col starts from 0 to 19.Each cell in the table represent either 0 or 1. 0 rep seat available, 1 repr seat reserved. Seat allocation starts from highest to lowest.And row j is highest, i is second highest and so on.Max 20 seats can be booked at a time. if seat is available print the seat no like. like "B2" i.e (2 row, 3 col) otherwise Print "Seats are not available." and we must book consecutive seats only.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: ankitnigam is an unknown quantity at this point 
Solved Threads: 0
ankitnigam ankitnigam is offline Offline
Newbie Poster

Need program to solution

 
0
  #10
25 Days Ago
Write a function for seat allocate & seat reserved.Seat allocate array and seat reserver array.Seat allocate array is of 10*20 & each row & column represent A1,A2....;B1,B2.....;J1,J2 & so on i.e row are A to J whereas col starts from 0 to 19.Each cell in the table represent either 0 or 1. 0 rep seat available, 1 repr seat reserved. Seat allocation starts from highest to lowest.And row j is highest, i is second highest and so on.Max 20 seats can be booked at a time. if seat is available print the seat no like. like "B2" i.e (2 row, 3 col) otherwise Print "Seats are not available." and we must book consecutive seats only.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC