seg fault that doesnt make sense

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

Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

seg fault that doesnt make sense

 
0
  #1
Aug 11th, 2005
kinda like the last program.. just does different things..

anyway, im getting a seg fault i dont understand one bit
its off of the menu function.

when you input an invalid number like 8 - it says invalid
but if you input 7 to quit the program it seg faults.

also, if you run one of the other menu options... it will go into that
function and it will ask the desired question but when you type
a value in it will seg fault.

is my input incorrect?
thanks.

  1. /*This program will work as a database for storing employee
  2. information for a company. It will work as a binary tree.
  3. The company information will be read in as a data file:
  4.  
  5.   proj4.dat
  6.  
  7. The data will then be available for search. After the search
  8. is completed, the information will be printed to the screen
  9. and also will be saved into a data file:
  10.  
  11.   results.dat
  12.  
  13. */
  14.  
  15. #include <iostream>
  16. #include <cstdlib>
  17. #include <fstream>
  18. using namespace std;
  19.  
  20. class binaryTree {
  21.  
  22. public:
  23. // constructor, destructor, and menu functions
  24. binaryTree ();
  25. ~binaryTree ();
  26. void menu ();
  27.  
  28. private:
  29. // struct used to gather information for a data file
  30. struct info {
  31.  
  32. string employee;
  33. string lastName;
  34. string firstName;
  35. char middleInitial;
  36. string state;
  37. string zipcode;
  38. string department;
  39. int yearsWorked;
  40. int salary;
  41. struct info * right; //right pointer
  42. struct info * left; // left pointer
  43. };
  44.  
  45. info * root; // base of the tree
  46.  
  47. // functions used in the program
  48. void information (info &);
  49. void makeDataFile (info &);
  50. void insert (info *, info &);
  51. void retrieveEmployee (info *);
  52. void retrieveLast (info *);
  53. void retrieveFirst (info *);
  54. void retrieveState (info *);
  55. void retrieveDepartment (info *);
  56. void retrieveZip (info *);
  57.  
  58. };
  59.  
  60. binaryTree::binaryTree() {
  61. // constructor to declare pointers used as null
  62.  
  63. info * root = NULL;
  64. info * right = NULL;
  65. info * left = NULL;
  66.  
  67. }
  68.  
  69. binaryTree::~binaryTree() {
  70. // simple destructor that doesnt do much
  71. // didnt know what to put here since we
  72. // didnt do a delete function
  73. delete root;
  74.  
  75. }
  76.  
  77. void binaryTree::information(info & person) {
  78. // this function is used for input
  79. // person is a call by reference to
  80. // the struct is used here to store
  81. // the information that is necessary
  82. // the inputs are done from a data file
  83. // named proj4.dat
  84. ifstream file; // creates instance of ifstream
  85. file.open ("proj4.dat");
  86.  
  87. file >> person.employee;
  88. file >> person.lastName;
  89. file >> person.firstName;
  90. file >> person.middleInitial;
  91. file >> person.state;
  92. file >> person.zipcode;
  93. file >> person.department;
  94. file >> person.yearsWorked;
  95. file >> person.salary;
  96.  
  97. file.close();
  98.  
  99. }
  100.  
  101. void binaryTree::makeDataFile (info & person) {
  102. // this function is used to make a data
  103. // file from the output generated when a
  104. // search took place. person is call by
  105. // reference and is used to bring in the
  106. // data to place in output.
  107. // the results can be found in results.dat
  108. ofstream output; // creates instance of ofstream
  109. output.open("results.dat");
  110.  
  111. output << person.employee;
  112. output << person.lastName;
  113. output << person.firstName;
  114. output << person.middleInitial;
  115. output << person.state;
  116. output << person.zipcode;
  117. output << person.department;
  118. output << person.yearsWorked;
  119. output << person.salary;
  120.  
  121. output.close();
  122.  
  123. }
  124.  
  125. void binaryTree::insert(info * tree, info & person) {
  126. // this will create the tree and insert the information into
  127. // the tree as needed. a pointer of struct info type referred
  128. // to as tree is call by value. the person parameter is call by
  129. // reference and brings in the information you have inputed and
  130. // places it into the tree
  131. if (tree == NULL) {
  132.  
  133. tree = new info;
  134. tree -> left = NULL;
  135. tree -> right = NULL;
  136. tree -> employee = person.employee;
  137. tree -> lastName = person.lastName;
  138. tree -> firstName = person.firstName;
  139. tree -> middleInitial = person.middleInitial;
  140. tree -> state = person.state;
  141. tree -> zipcode = person.zipcode;
  142. tree -> department = person.department;
  143. tree -> yearsWorked = person.yearsWorked;
  144. tree -> salary = person.salary;
  145.  
  146. }
  147. }
  148.  
  149. void binaryTree::retrieveEmployee(info * tree) {
  150. // this function will print out all the information
  151. // for the employee that is inputed by the user
  152. // a pointer of struct info type referred
  153. // to as tree is call by value.
  154.  
  155. // The search function is for employee number
  156. string input;
  157. cout << "Please input the ID number you wish to search for: ";
  158. cin >> input;
  159. while (root != NULL) {
  160. if (input == tree -> employee) {
  161. retrieveEmployee (tree -> left); //searches left child
  162. cout << tree -> employee;
  163. cout << tree -> lastName;
  164. cout << tree -> firstName;
  165. cout << tree -> middleInitial;
  166. cout << tree -> state;
  167. cout << tree -> zipcode;
  168. cout << tree -> department;
  169. cout << tree -> yearsWorked;
  170. cout << tree -> salary;
  171. retrieveEmployee (tree -> right); // searches right child
  172. }
  173. else {
  174. cout << "There is no information that meets search critera";
  175. cout << endl;
  176. break;
  177. }
  178. }
  179. info person; // creates an instance of info called person
  180. makeDataFile(person); // calls makeDataFile function
  181. }
  182.  
  183. void binaryTree::retrieveLast (info * tree) {
  184. // this function will print out all the information
  185. // for the employee that is inputed by the user
  186. // a pointer of struct info type referred
  187. // to as tree is call by value.
  188.  
  189. // The search function is for last name
  190. string input;
  191. cout << "Please input the last name you wish to search for: ";
  192. cin >> input;
  193. while (root != NULL) {
  194. if (input == tree -> lastName) {
  195. retrieveEmployee (tree -> left); //searches left child
  196. cout << tree -> employee;
  197. cout << tree -> lastName;
  198. cout << tree -> firstName;
  199. cout << tree -> middleInitial;
  200. cout << tree -> state;
  201. cout << tree -> zipcode;
  202. cout << tree -> department;
  203. cout << tree -> yearsWorked;
  204. cout << tree -> salary;
  205. retrieveEmployee (tree -> right); // searches right child
  206. }
  207. else {
  208. cout << "There is no information that meets search critera";
  209. cout << endl;
  210. break;
  211. }
  212. }
  213. info person; // creates an instance of info called person
  214. makeDataFile(person); // calls makeDataFile function
  215. }
  216.  
  217. void binaryTree::retrieveFirst (info * tree) {
  218. // this function will print out all the information
  219. // for the employee that is inputed by the user
  220. // a pointer of struct info type referred
  221. // to as tree is call by value.
  222.  
  223. // The search function is for first name
  224. string input;
  225. cout << "Please input the first name you wish to search for: ";
  226. cin >> input;
  227. while (root != NULL) {
  228. if (input == tree -> firstName) {
  229. retrieveEmployee (tree -> left); //searches left child
  230. cout << tree -> employee;
  231. cout << tree -> lastName;
  232. cout << tree -> firstName;
  233. cout << tree -> middleInitial;
  234. cout << tree -> state;
  235. cout << tree -> zipcode;
  236. cout << tree -> department;
  237. cout << tree -> yearsWorked;
  238. cout << tree -> salary;
  239. retrieveEmployee (tree -> right); // searches right child
  240. }
  241. else {
  242. cout << "There is no information that meets search critera";
  243. cout << endl;
  244. break;
  245. }
  246. }
  247. info person; // creates an instance of info called person
  248. makeDataFile(person); // calls makeDataFile function
  249. }
  250.  
  251. void binaryTree::retrieveState (info * tree) {
  252. // this function will print out all the information
  253. // for the employee that is inputed by the user
  254. // a pointer of struct info type referred
  255. // to as tree is call by value.
  256.  
  257. // The search function is for state
  258. string input;
  259. cout << "Please input the state you wish to search for: ";
  260. cin >> input;
  261. while (root != NULL) {
  262. if (input == tree -> state) {
  263. retrieveEmployee (tree -> left); //searches left child
  264. cout << tree -> employee;
  265. cout << tree -> lastName;
  266. cout << tree -> firstName;
  267. cout << tree -> middleInitial;
  268. cout << tree -> state;
  269. cout << tree -> zipcode;
  270. cout << tree -> department;
  271. cout << tree -> yearsWorked;
  272. cout << tree -> salary;
  273. retrieveEmployee (tree -> right); // searches right child
  274. }
  275. else {
  276. cout << "There is no information that meets search critera";
  277. cout << endl;
  278. break;
  279. }
  280. }
  281. info person; // creates an instance of info called person
  282. makeDataFile(person); // calls makeDataFile function
  283. }
  284.  
  285. void binaryTree::retrieveDepartment (info * tree) {
  286. // this function will print out all the information
  287. // for the employee that is inputed by the user
  288. // a pointer of struct info type referred
  289. // to as tree is call by value.
  290.  
  291. // The search function is for department
  292. string input;
  293. cout << "Please input the department you wish to search for: ";
  294. cin >> input;
  295. while (root != NULL) {
  296. if (input == tree -> department) {
  297. retrieveEmployee (tree -> left); //searches left child
  298. cout << tree -> employee;
  299. cout << tree -> lastName;
  300. cout << tree -> firstName;
  301. cout << tree -> middleInitial;
  302. cout << tree -> state;
  303. cout << tree -> zipcode;
  304. cout << tree -> department;
  305. cout << tree -> yearsWorked;
  306. cout << tree -> salary;
  307. retrieveEmployee (tree -> right); // searches right child
  308. }
  309. else {
  310. cout << "There is no information that meets search critera";
  311. cout << endl;
  312. break;
  313. }
  314. }
  315. info person; // creates an instance of info called person
  316. makeDataFile(person); // calls makeDataFile function
  317. }
  318.  
  319. void binaryTree::retrieveZip (info * tree) {
  320. // this function will print out all the information
  321. // for the employee that is inputed by the user
  322. // a pointer of struct info type referred
  323. // to as tree is call by value.
  324.  
  325. // The search function is for zipcode
  326. string input;
  327. cout << "Please input the zipcode you wish to search for: ";
  328. cin >> input;
  329. while (root != NULL) {
  330. if (input == tree -> zipcode) {
  331. retrieveEmployee (tree -> left); //searches left child
  332. cout << tree -> employee;
  333. cout << tree -> lastName;
  334. cout << tree -> firstName;
  335. cout << tree -> middleInitial;
  336. cout << tree -> state;
  337. cout << tree -> zipcode;
  338. cout << tree -> department;
  339. cout << tree -> yearsWorked;
  340. cout << tree -> salary;
  341. retrieveEmployee (tree -> right); // searches right child
  342. }
  343. else {
  344. cout << "There is no information that meets search critera";
  345. cout << endl;
  346. break;
  347. }
  348. }
  349. info person; // creates an instance of info called person
  350. makeDataFile(person); // calls makeDataFile function
  351. }
  352.  
  353. void binaryTree::menu() {
  354. // the menu function will print out a menu and let the user
  355. // pick the option of choice. it will then call the function
  356. // that is requested. it is set up to handle invalid entries
  357. // no data parameters are used for this function
  358. int choice = 0;
  359. info person; // declares instance of info called person
  360. information(person); // calls information function
  361. insert(root, person); // fills tree with the information
  362.  
  363. while (choice != 7) { // ends program if choice equals 7
  364. cout << endl;
  365. cout << "Welcome to the business. " << endl;
  366. cout << "Please choose your desired action. " << endl;
  367. cout << "1. Search for a specific employee ID. " << endl;
  368. cout << "2. Search by last name. " << endl;
  369. cout << "3. Search by first name. " << endl;
  370. cout << "4. Search by state. " << endl;
  371. cout << "5. Search by department. " << endl;
  372. cout << "6. Search by zip code. " << endl;
  373. cout << "7. Exit the program. " << endl;
  374. cin >> choice;
  375. cout << endl;
  376.  
  377. if (choice == 1) { // displays information if user inputed
  378. // employee number matches one on the list
  379. info tree; // creates instance of info called tree
  380. retrieveEmployee(root);
  381. }
  382. else if (choice == 2) { // displays information if user inputed
  383. // last name matches one on the list
  384. info tree; // creates instance of info called tree
  385. retrieveLast(root);
  386. }
  387. else if (choice == 3) { // displays information if user inputed
  388. // first name matches one on the list
  389. info tree; // creates instance of info called tree
  390. retrieveFirst(root);
  391. }
  392. else if (choice == 4) { // displays information if user inputed
  393. // state matches one on the list
  394. info tree; // creates instance of info called tree
  395. retrieveState(root);
  396. }
  397. else if (choice == 5) { // displays information if user inputed
  398. // department matches one on the list
  399. info tree; // creates instance of info called tree
  400. retrieveDepartment(root);
  401. }
  402. else if (choice == 6) { // displays information if user inputed
  403. // zip code matches one on the list
  404. info tree; // creates instance of info called tree
  405. retrieveZip(root);
  406. }
  407. else if (choice == 7) // terminates the while loop causing
  408. // the program to end
  409. break;
  410. else
  411. // error statement
  412. // takes you back to menu
  413. cout << "Invalid option. Please retry." << endl;
  414. }
  415. }
  416.  
  417. int main () {
  418.  
  419. binaryTree binTree; // declares an instance of the class binaryTree
  420. binTree.menu(); // calls the menu function
  421.  
  422. binTree.~binaryTree(); // calls the destructor on close
  423.  
  424. return 0;
  425.  
  426. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: seg fault that doesnt make sense

 
0
  #2
Aug 12th, 2005
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: seg fault that doesnt make sense

 
0
  #3
Aug 12th, 2005
i still got a seg fault but it was good enough to turn it in.
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