Function Returns Incorrectly

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

Join Date: Apr 2008
Posts: 27
Reputation: DevC++4.9.9.2 is an unknown quantity at this point 
Solved Threads: 1
DevC++4.9.9.2 DevC++4.9.9.2 is offline Offline
Light Poster

Function Returns Incorrectly

 
0
  #1
Nov 5th, 2008
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.


  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, 1 views)
File Type: h stack.h (735 Bytes, 1 views)
File Type: txt data (2).txt (49 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Function Returns Incorrectly

 
0
  #2
Nov 5th, 2008
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?
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