944,196 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2527
  • C++ RSS
Jul 27th, 2005
0

program dying?? any ideas why?

Expand Post »
As far as i thought, this should work?? there is a spot or 2 i am concerned if i did it right though. Any ideas would be nice.
It fatally crashes when ran in windows, didnt try in linux due to the fact i know it wouldnt work there either im sure.




C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct node {
  7. node * prev;
  8. int x;
  9. int y;
  10. char marker;
  11. };
  12.  
  13. class stack {
  14.  
  15. public:
  16.  
  17. stack ();
  18. void push (int, int, char);
  19. void pop ();
  20. int coordinates (int&, int&);
  21.  
  22.  
  23. private:
  24.  
  25. node * top;
  26.  
  27. };
  28.  
  29.  
  30. stack::stack () {
  31.  
  32. top = NULL;
  33. top -> x = 0;
  34. top -> y = 5;
  35.  
  36. }
  37.  
  38.  
  39. void stack::push (int x, int y, char spotmarker) { // add a new node onto the stack
  40.  
  41. if (top == NULL) {
  42.  
  43. node * temp = new node;
  44. temp -> x = x;
  45. temp -> y = y;
  46. temp -> marker = spotmarker;
  47. temp -> prev = top;
  48. top = temp;
  49. }
  50.  
  51. else {
  52.  
  53. node * temp = new node;
  54. temp -> x = x;
  55. temp -> y = y;
  56. temp -> marker = spotmarker;
  57. temp -> prev = NULL;
  58. top = temp;
  59.  
  60. }
  61.  
  62. }
  63.  
  64.  
  65. void stack::pop () { // remove the node off the stack
  66.  
  67. node * temp = top;
  68. top = temp -> prev;
  69. delete temp;
  70.  
  71. }
  72.  
  73. int stack::coordinates(int &positionX, int &positionY) { // used to find current position
  74.  
  75. positionX = top -> x;
  76. positionY = top -> y;
  77.  
  78. }
  79.  
  80.  
  81.  
  82. int main () {
  83.  
  84. stack myStack; // creates instance of stack class
  85. char maze[10][10];
  86. int x, y;
  87. int xCurrent, yCurrent;
  88. ifstream file;
  89. ofstream output;
  90.  
  91. file.open ("maze.txt");
  92.  
  93. for (y = 0; y < 10; y++){
  94.  
  95. for (x = 0; x < 10; x++){
  96.  
  97. file >> maze[x][y];
  98. cout << maze[x][y];
  99.  
  100. }
  101.  
  102. cout << endl;
  103.  
  104. }
  105.  
  106. file.close ();
  107. cout << endl;
  108.  
  109. myStack.push('*', 0, 5);
  110. while (xCurrent < 10 && yCurrent < 10){
  111.  
  112. (x, y) = myStack.coordinates(x, y); // IS THIS LEGAL?? IF NOT HOW DO YOU FIX IT?
  113.  
  114. if(maze[x+1][y] == 'X' || maze[x+1][y] == ' '){
  115.  
  116. x = x+1;
  117. maze[x][y] = '*';
  118. myStack.push('*', x, y);
  119.  
  120. }
  121.  
  122. else if(maze[x][y+1] == 'X' || maze[x][y+1] == ' '){
  123.  
  124. y = y+1;
  125. maze[x][y] = '*';
  126. myStack.push('*', x, y);
  127.  
  128. }
  129.  
  130. else if(maze[x][y-1] == 'X' || maze[x][y-1] == ' '){
  131.  
  132. y = y-1;
  133. maze[x][y] = '*';
  134. myStack.push('*', x, y);
  135.  
  136. }
  137.  
  138. else if(maze[x-1][y] == 'X' || maze[x-1][y] == ' '){
  139.  
  140. x = x-1;
  141. maze[x][y] = '*';
  142. myStack.push('*', x, y);
  143.  
  144. }
  145.  
  146. else{
  147.  
  148. myStack.pop();
  149.  
  150. }
  151.  
  152. xCurrent = x;
  153. yCurrent = y;
  154.  
  155. }
  156.  
  157. output.open ("pathtaken.dat");
  158.  
  159. for (y = 0; y < 10; y++){
  160.  
  161. for (x = 0; x < 10; x++){
  162.  
  163. output << maze[x][y];
  164. cout << maze[x][y];
  165.  
  166. }
  167.  
  168. cout << endl;
  169.  
  170. }
  171.  
  172. output.close ();
  173.  
  174. cin >> x;
  175. return 0;
  176.  
  177. }
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 28th, 2005
0

Re: program dying?? any ideas why?

code complete with comments... it compiles.. it prints out 2 mazes as requested.. but doesnt do anything to the second one...
skips my if statements to replace characters...
reads in x and y as 42, 10
when it should be 0,5 i think
where do i change this or how do i fix it?

any help would be nice... thank you




C++ Syntax (Toggle Plain Text)
  1. /***************************************************************************
  2.  
  3. The purpose of this program is to move through a given test maze.
  4. You start at the starting point and work your way to the end of a
  5. 10 X 10 maze. The maze is read in through a data file. The original
  6. maze is printed onto the screen and then is worked through. There is
  7. also a data file that will give you the path taken with *'s representing
  8. where you have travelled inside the maze. This file will essentially
  9. be the same maze as you read in with the exception of the *'s replacing
  10. the X's. The final path will also be printed out to the screen.
  11.  
  12.  
  13. File "testmaze.dat" needed to read in the maze.
  14. Output can be found in the file "pathtaken.dat".
  15.  
  16.  
  17. Stack class and header file are written into this file and not
  18. seperate files for the simple sake of making submission easier.
  19.  
  20. ***************************************************************************/
  21.  
  22.  
  23. // defining the needed libraries
  24.  
  25. #include <iostream> // used for cin/cout
  26. #include <cstdlib> // used for NULL
  27. #include <fstream> // used for file input and output
  28. using namespace std;
  29.  
  30. struct node {
  31. node * prev;
  32. // prev is used instead of next for
  33. // readability due to the fact that
  34. // you have to backtrack through the maze
  35. int x;
  36. int y;
  37. char marker;
  38. };
  39.  
  40. class stack {
  41.  
  42. public:
  43. // functions that are needed for this stack
  44. // push will add new information to the top of the stack
  45. // pop will take off the information
  46. // coordinates will return where you are located
  47. stack ();
  48. void push (int, int, char);
  49. void pop ();
  50. void coordinates (int&, int&);
  51.  
  52.  
  53. private:
  54. // top works where we would normally have next
  55. // however we define it as top since we are using
  56. // a stack and stacks only interact with the top element
  57. node * top;
  58.  
  59. };
  60.  
  61.  
  62. // functions for the stack class
  63.  
  64.  
  65. stack::stack () { // constructor used to initialize values
  66.  
  67. top = NULL;
  68. top = new node;
  69. top -> x = 0;
  70. top -> y = 5;
  71.  
  72. }
  73.  
  74.  
  75. void stack::push (int x, int y, char spotmarker) {
  76. // adds a new node onto the stack
  77. // int x and int y are used to keep track of your position
  78. // spotmarker is used to replace X's you have travelled on
  79. // with *'s to note your final path
  80.  
  81. if (top == NULL) {
  82.  
  83. node * temp = new node;
  84. temp -> x = x;
  85. temp -> y = y;
  86. temp -> marker = spotmarker;
  87. temp -> prev = NULL;
  88. top = temp;
  89. }
  90.  
  91. else {
  92.  
  93. node * temp = new node;
  94. temp -> x = x;
  95. temp -> y = y;
  96. temp -> marker = spotmarker;
  97. temp -> prev = top;
  98. top = temp;
  99.  
  100. }
  101.  
  102. }
  103.  
  104.  
  105. void stack::pop () {
  106. // remove the node off the stack
  107. // this is used for backtracking
  108.  
  109. node * temp = top;
  110. top = temp -> prev;
  111. delete temp;
  112.  
  113. }
  114.  
  115. void stack::coordinates(int &positionX, int &positionY) {
  116. // used to find current position
  117. // int positionX and int positionY are call by reference variables
  118. // they return the position you are currently occupying
  119.  
  120. positionX = top -> x;
  121. positionY = top -> y;
  122.  
  123. }
  124.  
  125.  
  126. // main program
  127.  
  128.  
  129. int main () {
  130.  
  131. stack myStack; // creates instance of stack class
  132. char maze[10][10]; // two dimensional array for maze given in data file
  133. int x = 0, y = 0;
  134. int xCurrent = 0;
  135. ifstream file; // creates instance of input file
  136. ofstream output; // creates instance of output file
  137.  
  138. file.open ("testmaze.dat"); //reads in data file for starting maze
  139. cout << endl << endl;
  140. cout << "This is the maze you are trying to solve." << endl << endl;
  141. for (y = 0; y < 10; y++){ // makes the columns for the y coordinates
  142.  
  143. for (x = 0; x < 10; x++){ // makes rows of the x coordinates
  144.  
  145. file >> maze[x][y]; // reads the maze from the input file
  146. cout << maze[x][y]; //prints maze to screen
  147.  
  148. }
  149.  
  150. cout << endl;
  151.  
  152. }
  153.  
  154. file.close (); // closes input file testmaze.dat
  155. cout << endl;
  156. cout << "This is the path taken to solve the maze." << endl << endl;
  157. output.open ("pathtaken.dat"); //stores path information in this data file
  158. myStack.push('*', x, y);
  159.  
  160. while (xCurrent < 10){
  161.  
  162. // you are out of the array and maze
  163. // boundries if you reach 10
  164. // the array is 10 X 10 starting at 0 and
  165. // ending with element 9
  166.  
  167. myStack.coordinates(x, y); //retrieves coordinates
  168. cout << x << " " << y; // added to see what values were
  169. if(maze[x+1][y] == 'X'){ // go right
  170.  
  171. x = x+1;
  172. maze[x][y] = '*';
  173. myStack.push('*', x, y);
  174.  
  175. }
  176.  
  177. else if(maze[x][y+1] == 'X'){ // go up
  178.  
  179. y = y+1;
  180. maze[x][y] = '*';
  181. myStack.push('*', x, y);
  182.  
  183. }
  184.  
  185. else if(maze[x][y-1] == 'X'){ // go down
  186.  
  187. y = y-1;
  188. maze[x][y] = '*';
  189. myStack.push('*', x, y);
  190.  
  191. }
  192.  
  193. else if(maze[x-1][y] == 'X'){ // go left
  194.  
  195. x = x-1;
  196. maze[x][y] = '*';
  197. myStack.push('*', x, y);
  198.  
  199. }
  200.  
  201. else{
  202. cout << "bob" << endl; // added as a marker to see
  203. // if it was skipping if statements
  204. myStack.pop(); // backtracks through the maze if needed
  205.  
  206. }
  207.  
  208. xCurrent = x;
  209.  
  210. }
  211.  
  212.  
  213. for (y = 0; y < 10; y++){ // makes the columns for the y coordinates
  214.  
  215. for (x = 0; x < 10; x++){ // makes rows of the x coordinates
  216.  
  217. output << maze[x][y]; // stores the maze into the output file
  218. cout << maze[x][y]; // prints path taken to screen
  219.  
  220. }
  221.  
  222. cout << endl;
  223.  
  224. }
  225.  
  226. output.close (); // closes the output file pathtaken.dat
  227.  
  228. cin >> x;
  229. return 0;
  230.  
  231. }
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Jul 28th, 2005
0

Re: program dying?? any ideas why?

never mind... i found it..
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Nov 18th, 2005
0

Re: program dying?? any ideas why?

Quote originally posted by jhdobbins ...
never mind... i found it..
I know this an old post, but dude whats the slution.
Please POST.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nsmith1 is offline Offline
1 posts
since Jun 2005
Oct 20th, 2006
0

I need Help on this project !!!!

I coincidently have the same project which you were posting. Since you found the solution. Can you post your final code for me to take a look at it?
Thank you.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hoangtrung is offline Offline
1 posts
since Oct 2006
Oct 21st, 2006
0

Re: program dying?? any ideas why?

Dont ressurect old threads like you were some shaman.
Thread closed.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

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.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: c++ graphics programme
Next Thread in C++ Forum Timeline: What is the Command-Line Argument for???





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


Follow us on Twitter


© 2011 DaniWeb® LLC