build but wont run... hmmm please help.

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

Join Date: Apr 2007
Posts: 15
Reputation: Ortal is an unknown quantity at this point 
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

build but wont run... hmmm please help.

 
0
  #1
Jun 20th, 2007
i wrote this program to input 12 intigers (mustuse linked lists), sort it while its being inserted, then take the average, then take out the numbers that are greater then the average and make a new lists, then take a second list of 12 intigers, then lastly merge those 2 lists into one that eliminates duplicates.

the program will build but when i run it (2003 builder) i get 'fatal error' pop up as soon as i press enter after putting in the first intiger. It is very frustrating. There is nothing more specific, just fatal error. Can anyone find the issue? I am a newbie, please be patient with me


  1. #include <iostream>
  2. using namespace std;
  3. class node_type //dec of class
  4. {
  5. public:
  6. int num;
  7. node_type *next;
  8. };
  9. void SortInsert (node_type** head,int data, bool nodeup);
  10. int main ()
  11. {
  12. node_type *prev, *current, *current2, *newnode;
  13. node_type *head, *head2, *head_merged, *temp;
  14. int i, number;
  15. float sum, average;
  16. sum = 0;
  17. head2 = new node_type;
  18. head = new node_type;
  19. cout << "Please enter 12 integers\n";
  20. for (i=1; i<=12; i++)
  21. {
  22. cout << i << "=" <<endl << endl;
  23. cin >> number;
  24. SortInsert (&head, number, 0);
  25. }
  26. cout << "entered numbers:\n";
  27. current = head;
  28. while (current != NULL)
  29. {
  30. cout << current->num <<endl;
  31. sum += current->num;
  32. current = current->next;
  33. }
  34. average = sum/12;
  35. cout << "\n The average of the numbers you inputed is " << average << "\n";
  36. //here we remove numbers less then the average.
  37. current = head;
  38. while(current != NULL)
  39. {
  40. if (current->num < average)
  41. {
  42. if (current == head)
  43. head = current->next;
  44. else
  45. prev->next = current->next;
  46. }
  47. prev = current;
  48. current = current->next;
  49. }
  50. cout << "This is the list after we took out all of the number less than the average \n";
  51. current = head;
  52. while(current !=NULL)
  53. {
  54. cout << current->num <<endl;
  55. current = current ->next;
  56. }
  57. head2 = NULL;
  58. cout << "Now please enter 12 more numbers to be merged into the previous list\n";
  59. for (i=1; i<=12; i++)
  60. {
  61. cout << i << "=" << endl << endl;
  62. cin >> number;
  63. SortInsert (&head2, number, 0);
  64. }
  65.  
  66. cout << "Enterded Numbers: \n";
  67. current = head2;
  68. while(current !=NULL)
  69. {
  70. cout << current->num <<endl;
  71. current = current->next;
  72. }
  73. //here create a new list with data from the first 2 and remove dups.
  74. head_merged = NULL;
  75. current = head;
  76. while (current!= NULL)
  77. {
  78. SortInsert(&head_merged, current ->num, 1);
  79. current = current->next;
  80. }
  81. current = head2;
  82. while(current != NULL)
  83. {
  84. SortInsert(&head_merged, current ->num, 1);
  85. current = current->next;
  86. }
  87. cout << "The 2 lists merged together, duplicates removed:\n";
  88. current = head_merged;
  89. while(current != NULL)
  90. {
  91. cout <<current->num << endl;
  92. current = current->next;
  93. }
  94. cin >> number; //keep the window open after program runs
  95. return 0;
  96. }
  97.  
  98. //sorting stufff
  99. void SortInsert(node_type** head, int data, bool nodup)
  100. {
  101. node_type *current, *prev, *newnode;
  102. newnode = new node_type;
  103. newnode-> num = data;
  104. current = *head;
  105. while ((current != NULL && current->num <=data))
  106. {
  107. if(current->num == data && nodup)
  108. return;
  109. prev = current;
  110. current = current->next;
  111. }
  112. if(*head == current)
  113. {
  114. *head = newnode;
  115. newnode->next = current;
  116. }
  117. else if (current == NULL)
  118. {
  119. prev->next = newnode;
  120. newnode->next = NULL;
  121. }
  122. else
  123. {
  124. prev->next = newnode;
  125. newnode->next = current;
  126. }
  127. }
Last edited by Ancient Dragon; Jun 21st, 2007 at 6:28 am. Reason: corrected code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: build but wont run... hmmm please help.

 
0
  #2
Jun 21st, 2007
1. Write some comments in the code.
2. Nice of you to use code tags, did you know that you can also add the language name to it for syntax highlighting.
3. Problem might be that next is not initialized to NULL. So on line 110 after first iteration current is set to junk.
4. To find such errors, you gotta write some traces (couts with values of variables) in the code (or use a debugger like VS or Sun Workshop or CodeBlocks or gdb...)
Last edited by thekashyap; Jun 21st, 2007 at 1:24 am. Reason: Added #4
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: build but wont run... hmmm please help.

 
0
  #3
Jun 21st, 2007
I think thekashyap is right about uninitialized variables -- add a constructor to the node_type class that initializes the variables to 0.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 15
Reputation: Ortal is an unknown quantity at this point 
Solved Threads: 0
Ortal Ortal is offline Offline
Newbie Poster

Re: build but wont run... hmmm please help.

 
0
  #4
Jun 21st, 2007
can someone please give me an example of how to do what was mentioned? I am sorry, i am new at this.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: build but wont run... hmmm please help.

 
0
  #5
Jun 21st, 2007
Pickup any C++ book n it'll tell you what's a c'tor, this being such a basic thing, it's best if you learn rather than we giving the code..
Are you Agile.. ?
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