No output

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

Join Date: Sep 2007
Posts: 2
Reputation: seyetotto is an unknown quantity at this point 
Solved Threads: 0
seyetotto seyetotto is offline Offline
Newbie Poster

Re: searching and inserting node in a binary search tree

 
0
  #1
Dec 18th, 2007
Please I'm new to this forum and I need a help with my code.I have written it all and compiled but it doesnt seem to bring any output(I'm reading the input from a file on my system).Please bail me out, I need to turn it in soonest
  1. #include<iostream>
  2. #include<fstream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. struct node
  8. {
  9. string firstName; // firstName letters
  10.  
  11. string lastName; //lastName letters
  12.  
  13. string telnumber; // Telephone number
  14.  
  15. string housenumber; // fax number
  16.  
  17. string city; // city
  18.  
  19. string state; //state
  20.  
  21. string zipcode; //zipcode
  22.  
  23. node *link; // Pointer to next node
  24. };
  25.  
  26. node *head = NULL;
  27.  
  28.  
  29. //Function to add a node to the list
  30. //Sets the list head to newNode if list is empty
  31. //Add the newNOde to the next position in the list if list is not empty
  32. void add_node_at_end (node & newNode)
  33. {
  34. node *temp, *temp2; // Temporary pointers
  35.  
  36. // fill the newNOde with the incoming data
  37. temp = new node;
  38.  
  39. temp->firstName = newNode.firstName;
  40.  
  41. temp->lastName = newNode.lastName;
  42.  
  43. temp->telnumber = newNode.telnumber;
  44.  
  45. temp->housenumber = newNode.housenumber;
  46.  
  47. temp->city = newNode.city;
  48.  
  49. temp->state = newNode.state;
  50.  
  51. temp->zipcode = newNode.zipcode;
  52.  
  53. temp->link = NULL;
  54.  
  55. // Add this node to the list
  56. if (head == NULL)
  57. head = temp;
  58. else
  59. {
  60. temp2 = head;
  61. // if the list not empty!
  62. while (temp2->link != NULL)
  63. { temp2 = temp2->link;
  64. // Move to next link in chain
  65. }
  66. temp2->link = temp;
  67.  
  68. }
  69. }
  70.  
  71. ////////////////////////////////////////////
  72. //Function ito displays the output file
  73. ////////////////////////////////////////////
  74.  
  75. void DisplayList(ofstream & outfile)
  76. {
  77. node * temp = head;
  78.  
  79. do
  80. { if (temp == NULL)
  81. cout << "End of list" << endl;
  82. else
  83. { // Display details for what temp points to
  84. outfile << "firstName : " << temp->firstName << endl;
  85.  
  86. outfile << "lastName : " << temp->lastName << endl;
  87.  
  88. outfile << "telnumber : " << temp->telnumber << endl;
  89.  
  90. outfile << "housenumber : " << temp->housenumber << endl;
  91.  
  92. outfile << "city : " << temp->city << endl;
  93.  
  94. outfile << "state : " << temp->state << endl;
  95.  
  96. outfile << "zipcode : " << temp->zipcode << endl;
  97.  
  98. outfile << endl; // Blank line
  99.  
  100. // Move to next node (if present)
  101. temp = temp->link;
  102. }
  103. }
  104. while (temp != NULL);
  105.  
  106. }
  107.  
  108.  
  109. //Deleting node at head
  110. void delete_start_node()
  111. {
  112. node *temp;
  113. temp = head;
  114. head = head->link;
  115. delete temp;
  116. }
  117.  
  118. //deleting at end
  119.  
  120. void delete_end_node()
  121. {
  122. node *temp1, *temp2; // Temporary pointers
  123.  
  124. //Deleting a node at the end of the list
  125. if (head == NULL)
  126. cout << "The list is empty!" << endl;
  127. else
  128. {
  129. temp1 = head;
  130. if (temp1->link == NULL)
  131. {
  132. delete temp1;
  133. head = NULL;
  134. }
  135. else
  136. {
  137. while (temp1->link != NULL)
  138. {
  139. temp2 = temp1;
  140. temp1 = temp1->link;
  141. }
  142. delete temp1;
  143. temp2->link = NULL;
  144. }
  145. }
  146. }
  147.  
  148. void add_node_at_start (node & newNode)
  149. {
  150. node * temp = 0; // Temporary pointers
  151.  
  152. // Reserve space for new node and fill it with data
  153. temp = new node;
  154.  
  155. temp->firstName = newNode.firstName;
  156.  
  157. temp->lastName = newNode.lastName;
  158.  
  159. temp->telnumber = newNode.telnumber;
  160.  
  161. temp->housenumber = newNode.housenumber;
  162.  
  163. temp->city = newNode.city;
  164.  
  165. temp->state = newNode.state;
  166.  
  167. temp->zipcode = newNode.zipcode;
  168.  
  169.  
  170. temp->link = head;
  171.  
  172. head = temp;
  173. }
  174.  
  175.  
  176. void add_in_middle(node & newNode)
  177. {
  178. node * newNodePtr = 0; // Temporary pointers
  179.  
  180. // Reserve space for new node and fill it with data
  181. newNodePtr = new node;
  182.  
  183.  
  184. newNodePtr->firstName = newNode.firstName;
  185.  
  186. newNodePtr->lastName = newNode.lastName;
  187.  
  188. newNodePtr->telnumber = newNode.telnumber;
  189.  
  190. newNodePtr->housenumber = newNode.housenumber;
  191.  
  192. newNodePtr->city = newNode.city;
  193.  
  194. newNodePtr->state = newNode.state;
  195.  
  196. newNodePtr->zipcode = newNode.zipcode;
  197.  
  198. newNodePtr->link= head->link;
  199.  
  200. head->link =newNodePtr;
  201.  
  202. }
  203.  
  204. void delete_in_middle ()
  205. {
  206. node * temp; // Temporary pointer
  207.  
  208. // check to see if the list is empty
  209.  
  210. if (head!= NULL)
  211. {
  212. // check to see if there is more than 1 node in the list
  213.  
  214. if (head->link != NULL)
  215. {
  216. temp= head->link->link ;
  217.  
  218. delete head->link;
  219.  
  220. head->link = temp;
  221. }
  222. }
  223. }
  224.  
  225. int main(void)
  226. {
  227. node inputNode;
  228.  
  229. ifstream infile; // input file
  230.  
  231. infile.open("c:\\pbinput.txt");
  232.  
  233. if (!infile)
  234. {
  235. cerr << "Unable to open file inputfile.txt";
  236.  
  237. exit(1); // call system to stop
  238. }
  239.  
  240. ofstream outfile;
  241.  
  242. outfile.open("outputfile.txt");
  243.  
  244. if (!outfile)
  245. {
  246. cerr << "Unable to open file outputForPow.txt";
  247.  
  248. exit(1); // call system to stop
  249. }
  250.  
  251. outfile << "This file contains the input and output for the Power function " << endl << endl;
  252.  
  253.  
  254. int i = 0;
  255.  
  256. while (!infile.eof())
  257. {
  258. infile >> inputNode.lastName >> inputNode.firstName >> inputNode.telnumber >> inputNode.housenumber >> inputNode.city >> inputNode.state >> inputNode.zipcode ;
  259.  
  260. outfile << "Last name: " << inputNode.lastName << " first name: " << inputNode.firstName << " tel number: " << inputNode.telnumber << " house number: " << inputNode.housenumber << " city: " << inputNode.city << " state : " << inputNode.state << " zip code: " << inputNode.zipcode << endl;
  261.  
  262. if ((i % 3) == 0)
  263. {
  264. outfile << "Adding to end of list " << endl;
  265.  
  266. add_node_at_end (inputNode);
  267. }
  268. else if ((i %3) == 3)
  269. {
  270. outfile << "Adding to beginning of list " << endl;
  271.  
  272. add_node_at_start (inputNode);
  273. }
  274. else
  275. {
  276. outfile << "Adding to the middle of list " << endl;
  277.  
  278. add_in_middle(inputNode);
  279. }
  280.  
  281. i++;
  282.  
  283.  
  284. }
  285.  
  286. outfile << "displaying list after reading input: " << endl;
  287.  
  288. DisplayList(outfile);
  289. getch();
  290. outfile << "displaying list after deleting first node: " << endl;
  291.  
  292. delete_start_node();
  293.  
  294. DisplayList(outfile);
  295. getch();
  296. outfile << "displaying list after deleting last node: " << endl;
  297.  
  298. delete_end_node();
  299.  
  300. DisplayList(outfile);
  301. getch();
  302. outfile << "displaying list after deleting a node in the middle of the list: " << endl;
  303.  
  304. delete_in_middle();
  305.  
  306. DisplayList(outfile);
  307. getch();
  308.  
  309. infile.close();
  310.  
  311. outfile.close();
  312.  
  313. return(1);
  314. }
Last edited by Narue; Dec 18th, 2007 at 4:40 pm. Reason: Added code tags.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: seyetotto is an unknown quantity at this point 
Solved Threads: 0
seyetotto seyetotto is offline Offline
Newbie Poster

Re: searching and inserting node in a binary search tree

 
0
  #2
Dec 18th, 2007
I'm sorry, I forgot to include that it is a phonebook and implemented in a singly linked list
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: No output

 
0
  #3
Dec 18th, 2007
What does your input file contain? The program looks OK to me. However, it can not print anything if it does not have input file. lolz!
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: No output

 
0
  #4
Dec 18th, 2007
First, how does it compile without the <string> library included?

Once that's fixed, it does output, to the file, as you designed the program to do.

Your input, however, will not be correct, using the infile >> inputNode.lastName >> inputNode.firstName >> .... method. This gets one "word" at a time for each input, so street address and possibly city names will be broken up. If the input file is formatted one field per line (first name, last name, street address, etc), then you can use getline( ).

Once data is read in, you call many list functions. The program is mysteriously waiting for you to press a key to continue, with no indication that you must. It does seem to do what it's supposed to do.

Also, the line else if ((i %3) == 3) will never be true. What is the range of possible responses from the mod operator?

(added) You may be experiencing a hanging program with the input loop control you use:
  1. while (!infile.eof())
  2. {
  3. infile >> inputNode.lastName >> ....
eof will not be detected until you try to read after the last successful input, so input will hang, thus not completing the loop. Put the whole input line as the while( ) condition, and you should see something better occuring.
  1. while (infile >> inputNode.lastName >> .........)
  2. {
  3. //processing begins
Last edited by vmanes; Dec 18th, 2007 at 5:58 pm.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC