Can't seem to let user input necessary information

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

Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Can't seem to let user input necessary information

 
1
  #1
Aug 12th, 2009
Its my 1st time posting here... Do tell me if I am doing anything wrong.

  1. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  2. getline(cin, qtn);
  3. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  4. while(int(foundyq) != -1)
  5. {
  6. if(noTry != 0 && noTry != 1)
  7. {
  8. cout << "This is not a question. Re-type the question and add a question mark at the end." << endl;
  9. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  10. cin >> qtn;
  11. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  12. }
  13. else if(noTry == 1)
  14. {
  15. cin >> qtn;
  16. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  17. }
  18. else
  19. {
  20. cout << "This is not a question. Re-type the question and add a question mark at the end." << endl;
  21. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  22. }
  23. noTry++;
  24. }
  25. noTry = 0;
  26.  
  27. gbst.replace(nInfo, qtn);
  28. gbst.inorderTraversal();
  29. system ("PAUSE");

Basically, the codes I show above is to ask user to input a question which will replace a leave in my binary search tree then display in orderly.

My problem here is that after displaying "Please specify a question that has a yes answer for your object and a no answer for my guess:" , the program does not allow the user to input a question.

Results of the codes above:
Please specify a question that has a yes answer for your object and a no answer for my guess:
Book
Is it a living thing?
Human

I want the results to be like this instead:
Please specify a question that has a yes answer for your object and a no answer for my guess:
Is it an animal?
Book
Is it a living thing?
Is it an animal?
Last edited by charis89; Aug 12th, 2009 at 1:37 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Re: Can't seem to let user input necessary information

 
0
  #2
Aug 13th, 2009
Added a while loop and it works but now but because of the while loop, "Please specify a question that has a yes answer for your object and a no answer for my guess:" output is repeated. Ignoring that, I continued coding and encountered a runtime error that I have no idea how to solve. Desperately need to get it done soon so any help is greatly appreciated.

Error:
'BinaryTreeGame.exe': Loaded 'C:\Documents and Settings\Charissa\My Documents\Visual Studio 2008\Projects\BinaryTreeGame\Debug\BinaryTreeGame.exe', Symbols loaded.
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
First-chance exception at 0x104981ee in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
Unhandled exception at 0x104981ee in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcde5.
The program '[5816] BinaryTreeGame.exe: Native' has exited with code 0 (0x0).

I'm not sure where exactly the problem is so here's all my codes except for BinaryTree.h

BinarySearchTree.h
  1. //Header File Binary Search Tree
  2.  
  3. #ifndef H_binarySearchTree
  4. #define H_binarySearchTree
  5. #include <iostream>
  6. #include <cassert>
  7. #include "BinaryTree.h"
  8.  
  9. using namespace std;
  10.  
  11. template<class elemType>
  12. class BinarySearchTree: public BinaryTree<elemType>
  13. {
  14. public:
  15. bool search(const elemType& searchItem);
  16.  
  17. bool searchByIndex(const elemType& searchItem, vector<string> &data);
  18.  
  19. void replace(const elemType& searchItem, string qtn, vector<string> &data);
  20.  
  21. string getLNode(const elemType& searchItem);
  22.  
  23. string getRNode(const elemType& searchItem);
  24.  
  25. void insert(const elemType& insertItem);
  26.  
  27. void insert(const elemType& insertItem, string direct);
  28.  
  29. void insert(const elemType& insertItem1,const elemType& insertItem2 , vector<string> data);
  30.  
  31. void deleteNode(const elemType& deleteItem);
  32.  
  33. private:
  34. void deleteFromTree(nodeType<elemType>* &p);
  35. };
  36.  
  37. template<class elemType>
  38. bool BinarySearchTree<elemType>::searchByIndex(const elemType& searchItem, vector<string> &data)
  39. {
  40. nodeType<elemType> *current;
  41. bool found = false;
  42. int searchItemIndex, currentItemIndex;
  43.  
  44. for (size_t i = 0; i < data.size(); ++i)
  45. {
  46. if(searchItem == data[i])
  47. searchItemIndex = i;
  48. }
  49.  
  50. if(root == NULL)
  51. cerr<<"Cannot search the empty tree."<<endl;
  52. else
  53. {
  54. current = root;
  55.  
  56. while(current != NULL && !found)
  57. {
  58. if(current->info == searchItem)
  59. found = true;
  60. else
  61. for (size_t i = 0; i < data.size(); ++i)
  62. {
  63. if(current->info == data[i])
  64. currentItemIndex = i;
  65. }
  66. if(currentItemIndex > searchItemIndex)
  67. current = current->llink;
  68. else
  69. current = current->rlink;
  70. }//end while
  71. }//end else
  72.  
  73. return found;
  74. }//end search
  75.  
  76.  
  77. template<class elemType>
  78. void BinarySearchTree<elemType>::replace(const elemType& searchItem, string qtn, vector<string> &data)
  79. {
  80. nodeType<elemType> *current;
  81. bool found = false;
  82. int searchItemIndex, currentItemIndex;
  83.  
  84. for (size_t i = 0; i < data.size(); ++i)
  85. {
  86. if(searchItem == data[i])
  87. {
  88. searchItemIndex = i;
  89. }
  90. }
  91.  
  92. if(root == NULL)
  93. cerr<<"Tree is empty, nothing to replace."<<endl;
  94. else
  95. {
  96. current = root;
  97.  
  98. while(current != NULL && !found)
  99. {
  100. if(current->info == searchItem)
  101. {
  102. current->info = qtn;
  103. data[searchItemIndex] = qtn;
  104. }
  105. else
  106. for (size_t i = 0; i < data.size(); i++)
  107. {
  108. if(current->info == data[i])
  109. {
  110. currentItemIndex = i;
  111. }
  112. }
  113. if(currentItemIndex > searchItemIndex)
  114. {
  115. current = current->llink;
  116. }
  117. else
  118. current = current->rlink;
  119. }//end while
  120. }//end else
  121. }//end replaace
  122.  
  123. template<class elemType>
  124. string BinarySearchTree<elemType>::getLNode(const elemType& searchItem)
  125. {
  126. nodeType<elemType> *current;
  127. bool found = false;
  128. string nodeInfo = "";
  129.  
  130. if(root == NULL)
  131. cerr<<"Cannot search the empty tree."<<endl;
  132. else
  133. {
  134. current = root;
  135.  
  136. while(current != NULL && !found)
  137. {
  138. if(current->info == searchItem)
  139. {
  140. current = current->llink;
  141. nodeInfo = current->info;
  142. found = true;
  143. }
  144. else
  145. if(current->info > searchItem)
  146. current = current->llink;
  147. else
  148. current = current->rlink;
  149. }//end while
  150. }//end else
  151.  
  152. return nodeInfo;
  153. }//end getLNode
  154.  
  155. template<class elemType>
  156. string BinarySearchTree<elemType>::getRNode(const elemType& searchItem)
  157. {
  158. nodeType<elemType> *current;
  159. bool found = false;
  160. string nodeInfo = "";
  161.  
  162. if(root == NULL)
  163. cerr<<"Cannot search the empty tree."<<endl;
  164. else
  165. {
  166. current = root;
  167.  
  168. while(current != NULL && !found)
  169. {
  170. if(current->info == searchItem)
  171. {
  172. current = current->rlink;
  173. nodeInfo = current->info;
  174. found = true;
  175. }
  176. else
  177. if(current->info > searchItem)
  178. current = current->llink;
  179. else
  180. current = current->rlink;
  181. }//end while
  182. }//end else
  183.  
  184. return nodeInfo;
  185. }//end getRNode
  186.  
  187. template<class elemType>
  188. void BinarySearchTree<elemType>::insert(const elemType& insertItem)
  189. {
  190. nodeType<elemType> *current; //pointer to traverse the tree
  191. nodeType<elemType> *trailCurrent; //pointer behind current
  192. nodeType<elemType> *newNode; //pointer to create the node
  193.  
  194. newNode = new nodeType<elemType>;
  195. assert(newNode != NULL);
  196. newNode->info = insertItem;
  197. newNode->llink = NULL;
  198. newNode->rlink = NULL;
  199.  
  200. if(root == NULL)
  201. {
  202. root = newNode;
  203. }
  204. else
  205. {
  206. current = root;
  207.  
  208. while(current != NULL)
  209. {
  210. trailCurrent = current;
  211.  
  212. if(current->info == insertItem)
  213. {
  214. cerr<<"The insert item is already in the list -- ";
  215. cerr<<"duplicates are not allowed."<<endl;
  216. return;
  217. }
  218. else
  219. {
  220.  
  221. if(current->info > insertItem)
  222. {
  223. current = current->llink;
  224. }
  225. else
  226. {
  227. current = current->rlink;
  228. }
  229. }
  230.  
  231. }//end while
  232.  
  233. if(trailCurrent->info > insertItem)
  234. {
  235. trailCurrent->llink = newNode;
  236. }
  237. else
  238. {
  239. trailCurrent->rlink = newNode;
  240. }
  241. }
  242. }//end insert
  243.  
  244. template<class elemType>
  245. void BinarySearchTree<elemType>::insert(const elemType& insertItem, string direct)
  246. {
  247. nodeType<elemType> *current; //pointer to traverse the tree
  248. nodeType<elemType> *trailCurrent; //pointer behind current
  249. nodeType<elemType> *newNode; //pointer to create the node
  250.  
  251. newNode = new nodeType<elemType>;
  252. assert(newNode != NULL);
  253. newNode->info = insertItem;
  254. newNode->llink = NULL;
  255. newNode->rlink = NULL;
  256.  
  257. if(root == NULL)
  258. {
  259. root = newNode;
  260. }
  261. else
  262. {
  263. current = root;
  264.  
  265. while(current != NULL)
  266. {
  267. trailCurrent = current;
  268.  
  269. if(current->info == insertItem)
  270. {
  271. cerr<<"The insert item is already in the list -- ";
  272. cerr<<"duplicates are not allowed."<<endl;
  273. return;
  274. }
  275. else
  276. {
  277. if(direct == "left")
  278. {
  279. current = current->llink;
  280. }
  281. else
  282. {
  283. current = current->rlink;
  284. }
  285. }
  286.  
  287. }//end while
  288.  
  289. if(direct == "left")
  290. {
  291. trailCurrent->llink = newNode;
  292. }
  293. else
  294. {
  295. trailCurrent->rlink = newNode;
  296. }
  297. }
  298. }//end insert
  299.  
  300. template<class elemType>
  301. void BinarySearchTree<elemType>::insert(const elemType& insertItem1,const elemType& insertItem2 , vector<string> data)
  302. {
  303. nodeType<elemType> *current; //pointer to traverse the tree
  304. nodeType<elemType> *trailCurrent; //pointer behind current
  305. nodeType<elemType> *newNode1; //pointer to create the node
  306. nodeType<elemType> *newNode2;
  307.  
  308. newNode1 = new nodeType<elemType>;
  309. newNode2 = new nodeType<elemType>;
  310. assert(newNode1 != NULL);
  311. assert(newNode2 != NULL);
  312. newNode1->info = insertItem1;
  313. newNode2->info = insertItem2;
  314.  
  315. string findItem = "";
  316. int findItemIndex, currentItemIndex;
  317.  
  318. for (size_t i = 0; i < data.size(); ++i)
  319. {
  320. if(insertItem1 == data[i])
  321. {
  322. findItemIndex = i+1;
  323. findItem = data[i+1];
  324. }
  325. }
  326.  
  327. current = root;
  328.  
  329. while(current != NULL)
  330. {
  331. trailCurrent = current;
  332.  
  333. if(current->info == findItem)
  334. {
  335. cerr<<"The insert item is already in the list -- ";
  336. cerr<<"duplicates are not allowed."<<endl;
  337. return;
  338. }
  339. else
  340. {
  341. for (size_t i = 0; i < data.size(); ++i)
  342. {
  343. if(current->info == data[i])
  344. currentItemIndex = i;
  345. }
  346.  
  347. if(currentItemIndex > findItemIndex)
  348. {
  349. current = current->llink;
  350. }
  351. else
  352. {
  353. current = current->rlink;
  354. }
  355. }
  356. }//end while
  357.  
  358. trailCurrent->llink = newNode1;
  359. trailCurrent->rlink = newNode2;
  360.  
  361. }//end insert
  362. #endif
  1. #include<iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. #include "BinaryTree.h"
  10. #include "BinarySearchTree.h"
  11.  
  12. int main()
  13. {
  14. vector<string> dataList;
  15. BinarySearchTree<string> gbst;
  16. string nInfo = "Is it a living thing?", obj = "", qtn = "";
  17. char userAns = 'y';
  18. int qtnNo = 1, noTry = 0, foundIndex;
  19. size_t found, foundObj, foundyq;
  20.  
  21. //insert into vector
  22. dataList.push_back("book");
  23. dataList.push_back("Is it a living thing?");
  24. dataList.push_back("human");
  25.  
  26. //insert into binary tree
  27. gbst.insert("Is it a living thing?");
  28. gbst.insert("human" , "right");
  29. gbst.insert("book" , "left");
  30.  
  31. // display game 1st question
  32. cout << "You must think of a single object. \nPlease answer the following questions about this object." << endl;
  33. cout << "QUESTION: " << nInfo << " (Y or N): ";
  34.  
  35. found = nInfo.find_first_not_of("abcdefghijklmnopqrstuvwxyzI ");
  36. while(int(found) != -1)
  37. {
  38. cin >> userAns;
  39. switch (userAns)
  40. {
  41. cout << "QUESTION: " << nInfo << " (Y or N): " << endl;
  42. case 'y': case 'Y':
  43. if(qtnNo = 1)
  44. {
  45. nInfo = gbst.getRNode("Is it a living thing?");
  46. qtnNo++;
  47. }
  48. else
  49. {
  50. nInfo = gbst.getRNode(nInfo);
  51. }
  52. break;
  53.  
  54. case 'n': case 'N':
  55. if(qtnNo = 1)
  56. {
  57. nInfo = gbst.getLNode("Is it a living thing?");
  58. qtnNo++;
  59. }
  60. else
  61. {
  62. nInfo = gbst.getLNode(nInfo);
  63. }
  64. break;
  65.  
  66. default :
  67. cout<<"\nPlease only enter 'y' or 'n'";
  68. }
  69.  
  70. found = nInfo.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  71. }
  72. cout << "I guess that you object is a(n) " << nInfo << " (Y or N): ";
  73. cin >> userAns;
  74.  
  75. switch (userAns)
  76. {
  77. case 'y': case 'Y':
  78. cout << "Notice the superior intellect of the computer!" << endl;
  79. break;
  80.  
  81. case 'n': case 'N':
  82. cout << "What object were you thinking of? ";
  83. cin >> obj;
  84. foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  85.  
  86. while(int(foundObj) != -1)
  87. {
  88. if(noTry != 0 && noTry != 1)
  89. {
  90. cout << "No symbols please." << endl;
  91. cout << "What object were you thinking of? ";
  92. cin >> obj;
  93. foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  94. }
  95. else if(noTry == 1)
  96. {
  97. cin >> obj;
  98. foundObj = obj.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  99. }
  100. else
  101. {
  102. cout << "No symbols please." << endl;
  103. cout << "What object were you thinking of? ";
  104. }
  105.  
  106. noTry++;
  107. }
  108. noTry = 0;
  109.  
  110. while (qtn == "")
  111. {
  112. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  113. getline(cin, qtn);
  114. }
  115.  
  116. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  117. while(int(foundyq) == -1)
  118. {
  119. if(noTry != 0 && noTry != 1)
  120. {
  121. cout << "Re-type the question and add a question mark at the end." << endl;
  122. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  123. getline(cin, qtn);
  124. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  125. }
  126. else if(noTry == 1)
  127. {
  128. getline(cin, qtn);
  129. foundyq = qtn.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ");
  130. }
  131. else
  132. {
  133. cout << "Re-type the question and add a question mark at the end." << endl;
  134. cout << "Please specify a question that has a yes answer for your object and a no answer for my guess:" << endl;
  135. }
  136. noTry++;
  137. }
  138. noTry = 0;
  139.  
  140. for(size_t i = 0; i < dataList.size(); ++i)
  141. {
  142. if(dataList[i] == nInfo)
  143. {
  144. foundIndex = i;
  145. }
  146. }
  147.  
  148. dataList.insert(dataList.begin()+(foundIndex),nInfo);
  149. dataList.insert(dataList.begin()+(foundIndex+2),obj);
  150.  
  151. gbst.insert(nInfo, obj, dataList);
  152. gbst.replace(nInfo, qtn, dataList);
  153. for(size_t i = 0; i < dataList.size(); ++i)
  154. {
  155. cout << dataList[i] << " ";
  156. }
  157. cout<< endl;
  158. gbst.inorderTraversal();
  159. //left no ans (nInfo)
  160. //right yes ans (obj)
  161. break;
  162.  
  163. default :
  164. cout<<"\nPlease only enter 'y' or 'n'";
  165. }
  166. system ("PAUSE");
  167.  
  168. return 0;
  169. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can't seem to let user input necessary information

 
0
  #3
Aug 13th, 2009
Looking at your first post, it appears that you have a very common problem when mixing cin and getline. See this thread and see if that's about the behavior you are getting.

http://www.daniweb.com/forums/thread90228.html

When you have a cin statement, followed at some point by a getline statement, the cin statement will leave the '\n' in the input buffer, then the getline will take that '\n' as an answer and thus not pause. Clear the input buffer after the cin statement with this line and you should get the pause for input with getline:

  1. cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

I didn't really look at your second post because you said you had added something to "fix" your first problem and that caused another problem? Try removing your first attempt at fixing it and putting the above line after all of your cin statements and see if that fixes everything.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Re: Can't seem to let user input necessary information

 
0
  #4
Aug 13th, 2009
Thanks that solve my 1st problem. However, I ran it again, the second error still appear.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can't seem to let user input necessary information

 
0
  #5
Aug 13th, 2009
We can't compile and run the program unless you post BinaryTree.h. What are the conditions of the run-time error (when does it happen, what input, etc.)?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Re: Can't seem to let user input necessary information

 
0
  #6
Aug 13th, 2009
ok. I found the problem. I did not add "found=true;" in BinarySearchTree.h

  1. template<class elemType>
  2. void BinarySearchTree<elemType>::replace(const elemType& searchItem, string qtn, vector<string> &data)
  3. {
  4. nodeType<elemType> *current;
  5. bool found = false;
  6. int searchItemIndex, currentItemIndex;
  7.  
  8. for (size_t i = 0; i < data.size(); ++i)
  9. {
  10. if(searchItem == data[i])
  11. {
  12. searchItemIndex = i;
  13. }
  14. }
  15.  
  16. if(root == NULL)
  17. cerr<<"Tree is empty, nothing to replace."<<endl;
  18. else
  19. {
  20. current = root;
  21.  
  22. while(current != NULL && !found)
  23. {
  24. if(current->info == searchItem)
  25. {
  26. current->info = qtn;
  27. data[searchItemIndex] = qtn;
  28. found = true; // <--- forgot to add this earlier
  29. }
  30. else
  31. for (size_t i = 0; i < data.size(); i++)
  32. {
  33. if(current->info == data[i])
  34. {
  35. currentItemIndex = i;
  36. }
  37. }
  38. if(currentItemIndex > searchItemIndex)
  39. {
  40. current = current->llink;
  41. }
  42. else
  43. current = current->rlink;
  44. }//end while
  45. }//end else
  46. }//end replaace

Then i run the codes again. Now I got this error in my BinaryTree.h
  1. 'BinaryTreeGame.exe': Loaded 'C:\Documents and Settings\Charissa\My Documents\Visual Studio 2008\Projects\BinaryTreeGame\Debug\BinaryTreeGame.exe', Symbols loaded.
  2. 'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
  3. 'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
  4. 'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll'
  5. 'BinaryTreeGame.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll'
  6. First-chance exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
  7. Unhandled exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
  8. The program '[6796] BinaryTreeGame.exe: Native' has exited with code 0 (0x0).

The code stops here in BinaryTree.h
  1. template<class elemType>
  2. void BinaryTree<elemType>::inorder(nodeType<elemType> *p)
  3. {
  4. if(p != NULL)
  5. {
  6. inorder(p->llink); //<-- there is a yellow arrow pointing here
  7. cout<<p->info<<" "<<endl;;
  8. inorder(p->rlink);
  9. }
  10. }

here is my full BinaryTree.h
  1. //Header File Binary Tree
  2. #ifndef H_binaryTree
  3. #define H_binaryTree
  4.  
  5. #include <iostream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. //Definition of the node
  11. template<class elemType>
  12. struct nodeType
  13. {
  14. elemType info;
  15. nodeType<elemType> *llink;
  16. nodeType<elemType> *rlink;
  17. };
  18.  
  19. //Definition of the class
  20. template <class elemType>
  21. class BinaryTree
  22. {
  23. public:
  24. const BinaryTree<elemType>& operator=
  25. (const BinaryTree<elemType>&);
  26. //Overload the assignment operator.
  27. bool isEmpty();
  28.  
  29. void inorderTraversal();
  30.  
  31. void preorderTraversal();
  32.  
  33. void postorderTraversal();
  34.  
  35. int treeHeight();
  36.  
  37. int treeNodeCount();
  38.  
  39. int treeLeavesCount();
  40.  
  41. void destroyTree();
  42.  
  43. BinaryTree(const BinaryTree<elemType>& otherTree);
  44.  
  45. BinaryTree();
  46.  
  47. ~BinaryTree();
  48.  
  49. protected:
  50. nodeType<elemType> *root;
  51.  
  52. private:
  53. void copyTree(nodeType<elemType>* &copiedTreeRoot,
  54. nodeType<elemType>* otherTreeRoot);
  55.  
  56. void destroy(nodeType<elemType>* &p);
  57.  
  58. void inorder(nodeType<elemType> *p);
  59.  
  60. void preorder(nodeType<elemType> *p);
  61.  
  62. void postorder(nodeType<elemType> *p);
  63.  
  64. int height(nodeType<elemType> *p);
  65.  
  66. int max(int x, int y);
  67.  
  68. int nodeCount(nodeType<elemType> *p);
  69.  
  70. int leavesCount(nodeType<elemType> *p);
  71. };
  72.  
  73. //Definition of member functions
  74.  
  75. template<class elemType>
  76. BinaryTree<elemType>::BinaryTree()
  77. {
  78. root = NULL;
  79. }
  80.  
  81. template<class elemType>
  82. bool BinaryTree<elemType>::isEmpty()
  83. {
  84. return (root == NULL);
  85. }
  86.  
  87. template<class elemType>
  88. void BinaryTree<elemType>::inorderTraversal()
  89. {
  90. inorder(root);
  91. }
  92.  
  93. template<class elemType>
  94. void BinaryTree<elemType>::preorderTraversal()
  95. {
  96. preorder(root);
  97. }
  98.  
  99. template<class elemType>
  100. void BinaryTree<elemType>::postorderTraversal()
  101. {
  102. postorder(root);
  103. }
  104.  
  105. template<class elemType>
  106. int BinaryTree<elemType>::treeHeight()
  107. {
  108. return height(root);
  109. }
  110.  
  111. template<class elemType>
  112. int BinaryTree<elemType>::treeNodeCount()
  113. {
  114. return nodeCount(root);
  115. }
  116.  
  117. template<class elemType>
  118. int BinaryTree<elemType>::treeLeavesCount()
  119. {
  120. return leavesCount(root);
  121. }
  122.  
  123. template <class elemType>
  124. void BinaryTree<elemType>::copyTree
  125. (nodeType<elemType>* &copiedTreeRoot,
  126. nodeType<elemType>* otherTreeRoot)
  127. {
  128. if(otherTreeRoot == NULL)
  129. copiedTreeRoot = NULL;
  130. else
  131. {
  132. copiedTreeRoot = new nodeType<elemType>;
  133. copiedTreeRoot->info = otherTreeRoot->info;
  134. copyTree(copiedTreeRoot->llink, otherTreeRoot->llink);
  135. copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink);
  136. }
  137. } //end copyTree
  138.  
  139. template<class elemType>
  140. void BinaryTree<elemType>::inorder(nodeType<elemType> *p)
  141. {
  142. if(p != NULL)
  143. {
  144. inorder(p->llink);
  145. cout<<p->info<<" "<<endl;;
  146. inorder(p->rlink);
  147. }
  148. }
  149.  
  150. template<class elemType>
  151. void BinaryTree<elemType>::preorder(nodeType<elemType> *p)
  152. {
  153. if(p != NULL)
  154. {
  155. cout<<p->info<<" ";
  156. preorder(p->llink);
  157. preorder(p->rlink);
  158. }
  159. }
  160.  
  161. template<class elemType>
  162. void BinaryTree<elemType>::postorder(nodeType<elemType> *p)
  163. {
  164. if(p != NULL)
  165. {
  166. postorder(p->llink);
  167. postorder(p->rlink);
  168. cout<<p->info<<" ";
  169. }
  170. }
  171.  
  172. //Overload the assignment operator
  173. template<class elemType>
  174. const BinaryTree<elemType>& BinaryTree<elemType>::
  175. operator=(const BinaryTree<elemType>& otherTree)
  176. {
  177.  
  178. if(this != &otherTree) //avoid self-copy
  179. {
  180. if(root != NULL) //if the binary tree is not empty,
  181. //destroy the binary tree
  182. destroy(root);
  183.  
  184. if(otherTree.root == NULL) //otherTree is empty
  185. root = NULL;
  186. else
  187. copyTree(root, otherTree.root);
  188. }//end else
  189.  
  190. return *this;
  191. }
  192.  
  193. template <class elemType>
  194. void BinaryTree<elemType>::destroy(nodeType<elemType>* &p)
  195. {
  196. if(p != NULL)
  197. {
  198. destroy(p->llink);
  199. destroy(p->rlink);
  200. delete p;
  201. p = NULL;
  202. }
  203. }
  204.  
  205. template <class elemType>
  206. void BinaryTree<elemType>::destroyTree()
  207. {
  208. destroy(root);
  209. }
  210.  
  211. //copy constructor
  212. template <class elemType>
  213. BinaryTree<elemType>::BinaryTree
  214. (const BinaryTree<elemType>& otherTree)
  215. {
  216. if(otherTree.root == NULL) //otherTree is empty
  217. root = NULL;
  218. else
  219. copyTree(root, otherTree.root);
  220. }
  221.  
  222. template <class elemType>
  223. BinaryTree<elemType>::~BinaryTree()
  224. {
  225. destroy(root);
  226. }
  227.  
  228. template<class elemType>
  229. int BinaryTree<elemType>::height(nodeType<elemType> *p)
  230. {
  231. if(p == NULL)
  232. return 0;
  233. else
  234. return 1 + max(height(p->llink), height(p->rlink));
  235. }
  236.  
  237. template<class elemType>
  238. int BinaryTree<elemType>::max(int x, int y)
  239. {
  240. if(x >= y)
  241. return x;
  242. else
  243. return y;
  244. }
  245.  
  246. #endif
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Re: Can't seem to let user input necessary information

 
0
  #7
Aug 13th, 2009
input:
y
n
lion
is it an animal?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Can't seem to let user input necessary information

 
0
  #8
Aug 13th, 2009
Originally Posted by charis89 View Post
input:
y
n
lion
is it an animal?
Upload the most recent versions of the three files as a zip attachment. I copied and pasted the code, but you have made changes. It'd be easiest to just upload the most recent version of everything as a zip file so everyone has the most recent versions and there's no guesswork.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Can't seem to let user input necessary information

 
0
  #9
Aug 13th, 2009
> First-chance exception at 0x004185bc in BinaryTreeGame.exe: 0xC0000005: Access violation reading location 0xcdcdcded.
Heap memory (in debug mode) gets filled with 0xcd when you free it.

> inorder(p->llink); //<-- there is a yellow arrow pointing here
p or p->llink is pointing to a node which no longer exists.

Did you miss a p = NULL when deleting a node?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: charis89 is an unknown quantity at this point 
Solved Threads: 0
charis89 charis89 is offline Offline
Newbie Poster

Re: Can't seem to let user input necessary information

 
0
  #10
Aug 14th, 2009
I did not delete any node. Only insert and change the value in 1 node in the replace method.
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC