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

#include <iostream>
using namespace std;
class node_type //dec of class
{
public:
 int num;
 node_type *next;
};
void SortInsert (node_type** head,int data, bool nodeup);
int main ()
{
 node_type *prev, *current, *current2, *newnode;
 node_type *head, *head2, *head_merged, *temp;
 int i, number;
 float sum, average;
 sum = 0;
 head2 = new node_type;
 head = new node_type;
 cout << "Please enter 12 integers\n";
 for (i=1; i<=12; i++)
 {
  cout << i << "=" <<endl << endl;
  cin >> number;
  SortInsert (&head, number, 0);
 }
 cout << "entered numbers:\n";
 current = head;
 while (current != NULL)
 {
  cout << current->num <<endl;
        sum += current->num;
  current = current->next;
 }
 average = sum/12;
 cout << "\n The average of the numbers you inputed is " << average << "\n";
 //here we remove numbers less then the average.
 current = head;
 while(current != NULL)
 {
  if (current->num < average)
  {
   if (current == head)
    head = current->next;
   else
    prev->next = current->next;
  }
  prev = current;
  current = current->next;
 }
 cout << "This is the list after we took out all of the number less than the average \n";
 current = head;
 while(current !=NULL)
 {
  cout << current->num <<endl;
  current = current ->next;
 }
 head2 = NULL;
 cout << "Now please enter 12 more numbers to be merged into the previous list\n";
 for (i=1; i<=12; i++)
 {
  cout << i << "=" << endl << endl;
  cin >> number;
  SortInsert (&head2, number, 0);
 }
 
 cout << "Enterded Numbers: \n";
 current = head2;
 while(current !=NULL)
 {
  cout << current->num <<endl;
  current = current->next;
 }
 //here create a new list with data from the first 2 and remove dups.
 head_merged = NULL;
 current = head;
 while (current!= NULL)
 {
  SortInsert(&head_merged, current ->num, 1);
  current = current->next;
 }
 current = head2;
 while(current != NULL)
 {
  SortInsert(&head_merged, current ->num, 1);
  current = current->next;
 }
 cout << "The 2 lists merged together, duplicates removed:\n";
 current = head_merged;
 while(current != NULL)
 {
  cout <<current->num << endl;
  current = current->next;
 }
 cin >> number; //keep the window open after program runs
 return 0;
}
 
//sorting stufff
void SortInsert(node_type** head, int data, bool nodup)
{
 node_type *current, *prev, *newnode;
 newnode = new node_type;
 newnode-> num = data;
 current = *head;
 while ((current != NULL && current->num <=data))
 {
  if(current->num == data && nodup)
   return;
  prev = current;
  current = current->next;
 }
 if(*head == current)
 {
  *head = newnode;
  newnode->next = current;
 }
 else if (current == NULL)
 {
  prev->next = newnode;
  newnode->next = NULL;
 }
 else
 {
  prev->next = newnode;
  newnode->next = current;
 }
}

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...)

I think thekashyap is right about uninitialized variables -- add a constructor to the node_type class that initializes the variables to 0.

can someone please give me an example of how to do what was mentioned? I am sorry, i am new at this.

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..

This article has been dead for over six months. Start a new discussion instead.