943,749 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 831
  • C++ RSS
Jun 20th, 2007
0

build but wont run... hmmm please help.

Expand Post »
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


C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Jun 21st, 2007
0

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

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
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 21st, 2007
0

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

I think thekashyap is right about uninitialized variables -- add a constructor to the node_type class that initializes the variables to 0.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Jun 21st, 2007
0

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

can someone please give me an example of how to do what was mentioned? I am sorry, i am new at this.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ortal is offline Offline
15 posts
since Apr 2007
Jun 21st, 2007
0

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

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..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: MSVC++ 2005: Find The Brightest Pixel..
Next Thread in C++ Forum Timeline: My First GUI / Animation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC