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;
int i, number;
float sum, average;
sum = 0;
cout << "Please enter 12 integers\n";
for (i=1; i<=12; i++)
{
cout << i << "=" <<endl << endl;
cin >> number;
}
cout << "entered numbers:\n";
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.
while(current != NULL)
{
if (current->num < average)
{
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";
while(current !=NULL)
{
cout << current->num <<endl;
current = current ->next;
}
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;
}

cout << "Enterded Numbers: \n";
while(current !=NULL)
{
cout << current->num <<endl;
current = current->next;
}
//here create a new list with data from the first 2 and remove dups.
while (current!= NULL)
{
current = current->next;
}
while(current != NULL)
{
current = current->next;
}
cout << "The 2 lists merged together, duplicates removed:\n";
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;
while ((current != NULL && current->num <=data))
{
if(current->num == data && nodup)
return;
prev = current;
current = current->next;
}
{
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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.