943,812 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 328
  • C++ RSS
Nov 5th, 2008
0

Function Returns Incorrectly

Expand Post »
Hey, my program is having trouble in the Solve() and Check() functions... basicaly, the Check() function is returning 1 or 0 based on different condtions, however after the 3rd or 4th time the value returned from function Check() is 1 and the if statement in function Solve() is interpreting it as 0 or false.... any ideas would be very helpful

Thank you

I have attached the program and a test file called data.


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include "stack.h"
  4. using namespace std;
  5. typedef NodeType* NodePtr;
  6.  
  7. struct NodeType
  8. {
  9. ItemType x;
  10. ItemType y;
  11. ItemType check;
  12. NodePtr next;
  13. };
  14.  
  15. Stack::Stack()
  16. {
  17. stackPtr = NULL;
  18. length = 0;
  19. }
  20. int Stack::topX()
  21. {
  22. return stackPtr->x;
  23. }
  24. int Stack::topY()
  25. {
  26. return stackPtr->y;
  27. }
  28. int Stack::topCheck()
  29. {
  30. return stackPtr->check;
  31. }
  32.  
  33. void Stack::push(ItemType x, ItemType y, ItemType check)
  34. {
  35. NodePtr ptr;
  36. ptr = new NodeType;
  37. ptr->x = x;
  38. ptr->y = y;
  39. ptr->check = check;
  40. ptr->next = stackPtr;
  41. stackPtr = ptr;
  42. length++;
  43. print();
  44. }
  45.  
  46. void Stack::pop()
  47. {
  48. NodeType *tempPtr;
  49. tempPtr = stackPtr;
  50. stackPtr = stackPtr->next;
  51. delete tempPtr;
  52. length--;
  53. print();
  54. }
  55.  
  56. bool Stack::isThere(ItemType x, ItemType y)
  57. {
  58. NodeType *Ptr;
  59. int q=0;
  60. Ptr = stackPtr;
  61. while((Ptr->x != x) && (Ptr->y != y))
  62. {
  63. if(Ptr == NULL)
  64. return 0;
  65. if(length == q)
  66. return 0;
  67. Ptr= Ptr->next;
  68. q++;
  69. }
  70. return 1;
  71. }
  72.  
  73. void Stack::print() const
  74. // Post: Items on the list are printed on the screen.
  75. {
  76. if (stackPtr == NULL)
  77. cout << "List is empty\n";
  78. else
  79. {
  80. NodeType *ptr;
  81. ptr = stackPtr;
  82. while(ptr != NULL)
  83. {
  84. cout << "PRINT\n";
  85. cout << "X: " << ptr->x << endl;
  86. cout << "y: " << ptr->y << endl;
  87. cout << "Check: " << ptr->check << endl << endl;
  88. ptr = ptr->next;
  89. }
  90. }
  91. }
  92.  
  93. //********************//
  94. //*End Implementation*//
  95. //********************//
  96.  
  97.  
  98. void getData(int**,int&,int&);
  99. void print(int**,int,int);
  100. bool Check(Stack&,int**,int&,int&,int&,int,int,int&);
  101. void Solve(Stack&,int**,int&,int&,int&,int,int,int&);
  102.  
  103. int main()
  104. {
  105. int **array;
  106. int row, column;
  107. int x=0,y=0,check=0,old=0;
  108. Stack myStack;
  109. myStack.push(x,y,check);
  110. string fileName;
  111. ifstream data;
  112.  
  113. ///////////////
  114. ////GetData////
  115. ///////////////
  116. cout << "Enter File Name\n";
  117. getline(cin, fileName);
  118. data.open(fileName.c_str());
  119. if (!data.is_open())
  120. cout << "Error opening file\n";
  121. else
  122. {
  123. data >> row >> column;
  124. array = new int*[column]; //Allocates memory for the array
  125. for (int q = 0; q < column; ++q)
  126. array[q] = new int[row];
  127. for(int y=0; y < row; y++)
  128. {
  129. for(int x=0; x < column; x++)
  130. {
  131. data >> array[y][x];
  132. }
  133. }
  134. }
  135. data.close();
  136. ///////////////
  137. //End GetData//
  138. ///////////////
  139.  
  140. print(array,row,column); //Before
  141. Solve(myStack,array,x,y,check,column,row,old);
  142. print(array,row,column); //After
  143.  
  144. system("PAUSE");
  145. return 0;
  146. }
  147.  
  148. void Solve(Stack &myStack,int **array,int &x,int &y,int &check,int column,int row,int &old)
  149. {
  150. if((y > column)&&(x > row))
  151. return;
  152. if((Check(myStack,array,x,y,check,column,row,old))== 0)
  153. {
  154. cout << "IF\n";
  155. check = myStack.topCheck();
  156. myStack.pop();
  157. x = myStack.topX();
  158. y = myStack.topY();
  159. old = check;
  160. }
  161. else
  162. {
  163. cout << "ELSE\n";
  164. myStack.push(x,y,check);
  165. switch(check)
  166. {
  167. case 1: old = 3;
  168. break;
  169. case 2: old = 4;
  170. break;
  171. case 3: old = 1;
  172. break;
  173. case 4: old = 2;
  174. break;
  175. }
  176. check = 1;
  177. }
  178. Solve(myStack,array,x,y,check,column,row,old);
  179. }
  180.  
  181. bool Check(Stack &myStack,int **array,int &x,int &y,int &check,int column,int row,int &old)
  182. {
  183. cout << "IN Check: ";
  184. cout << check << endl;
  185.  
  186. if(check > 4)
  187. {
  188. cout << "Return to solve\n";
  189. return 0;
  190. }
  191. if(check == old)
  192. check++;
  193. switch(check)
  194. {
  195. //Check Right
  196. case 1:
  197. if(((x+1) > column)||((array[y][x+1]) == 1))
  198. {
  199. check++;
  200. }
  201. else
  202. {
  203. //myStack.push((x+1),y,check);
  204. x++;
  205. cout << "return 1" << endl;
  206. return 1;
  207. }
  208. break;
  209. //Check Down
  210. case 2:
  211. if(((y+1) > row)||((array[y+1][x]) == 1))
  212. {
  213. check++;
  214. }
  215. else
  216. {
  217. //myStack.push(x,y,check);
  218. y++;
  219. cout << "return 1" << endl;
  220. return 1;
  221. }
  222. break;
  223. //Check Left
  224. case 3:
  225. if(((x-1) < 0)||((array[y][x-1]) == 1))
  226. {
  227. check++;
  228. }
  229. else
  230. {
  231. //myStack.push((x-1),y,check);
  232. x--;
  233. cout << "return 1" << endl;
  234. return 1;
  235. }
  236. break;
  237. //Check Up
  238. case 4:
  239. if(((y-1) < 0)||((array[y-1][x]) == 1))
  240. {
  241. check++;
  242. }
  243. else
  244. {
  245. //myStack.push(x,(y-1),check);
  246. y--;
  247. cout << "return 1" << endl;
  248.  
  249. return 1;
  250. }
  251. break;
  252. }
  253. Check(myStack,array,x,y,check,column,row,old);
  254. }
  255.  
  256. void print(int **array, int row, int column)
  257. {
  258. for(int y=0; y < row; y++)
  259. {
  260. cout << endl;
  261. for(int x=0; x < column; x++)
  262. {
  263. if(array[y][x] == 2)
  264. cout << "* ";
  265. else
  266. cout << array[y][x] << " ";
  267. }
  268. }
  269. cout << endl;
  270. }
Attached Files
File Type: cpp Maze6.cpp (5.5 KB, 3 views)
File Type: h stack.h (735 Bytes, 4 views)
File Type: txt data (2).txt (49 Bytes, 4 views)
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
DevC++4.9.9.2 is offline Offline
39 posts
since Apr 2008
Nov 5th, 2008
0

Re: Function Returns Incorrectly

Well, your Check function does not return value after self-recursive call (see the last statement w/o return). Didn't MinGW compiler diagnose this obvious error?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

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: How do I use methods outside of the main program?
Next Thread in C++ Forum Timeline: error: expected class-name before '{' token





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


Follow us on Twitter


© 2011 DaniWeb® LLC