hello all. i've made been a .cpp that is supposed to retrieve 2 lists from user, and then prints either one. this isn't all my code, but it is what i think is wrong. i've been killing my brain cells trying to figure out what is wrong with this but i just can't seem to pull it off. i'm actually disappointed that i had to resort to posting this... but is guess i got to know when to give up.
emplist *head = NULL; //ini. head pointer for list 1
emplist *curr = NULL; //ini. current pointer for list 1
emplist *head1 = NULL; //ini head pointer for list 2
emplist *curr1 = NULL; //ini current pointer for list 2
int main()
{
char a;
emplist e;
//I ask user to input data for emplist e... i obtain e.name, e.level and blablbla
if (e.level>5)
{
int x = 0;
addem(e, x); //anything wrong here???
}
else
{
int x = 1;
addem(e, x); //or here???
}
cout<<"Would you like to make another add? [Y/N]: ";
cin>>a;
for (curr = head; curr->nxt != NULL; curr = curr->nxt) //attempting to print list 1
{
cout<<"inside for befor idqry"<<endl;
curr->idqry();
cout<<"inside for after idqry"<<endl;
}
return 0;
}
forgot to include addem func... here it is:
void addem(emplist nemp, int x) //addem func
{
if (x = 0)
{
if (curr == NULL)
{
*curr = nemp;
head = curr;
curr->nxt;
}
else
{
curr->nxt = &nemp;
nemp.nxt = NULL;
}
}
else if (x = 1)
{
if (curr1 == NULL)
{
curr1 = &nemp;
head1 = curr1;
curr1->nxt;
}
void addem(emplist nemp, int x)
{
if (x = 0)
{
if (curr == NULL)
{
curr = &nemp; // was writing to the location of a null pointer!!
head = curr;
curr->nxt = NULL; // Set to null to end the list?
}
else
{
curr->nxt = &nemp;
nemp.nxt = NULL;
}
}
elseif (x = 1)
{
if (curr1 == NULL)
{
curr1 = &nemp;
head1 = curr1;
curr1->nxt = NULL;
}
else
{ curr1->nxt = &nemp;
nemp.nxt = NULL; // Set to null to end the list?
}
}
else
{
cout<<"CRITICAL ERROR!"<<endl;
}
}
int main()
{
char a;
emplist* e = new emplist; // use new to create a new emplist object
//I ask user to input data
if (e->level > 5) // changed accessor to -> because its a pointer now
{
int x = 0;
addem(e, x);
}
else
{
int x = 1;
addem(e, x);
}
cout << "Would you like to make another add? [Y/N]: ";
cin >> a;
for (curr = head; curr->nxt != NULL; curr = curr->nxt) //attempting to print list 1
{
cout << "inside for befor idqry" << endl;
curr->idqry();
cout << "inside for after idqry" << endl;
}
return 0;
}
thanks for the quick reply... i've fixed the issues u found.. THANKS FOR THAT. my c.pp is actually running and not crashing at any point, however it seems that both lists will only one node each.. and the data in both of them is identical (it is always the last user input)... do u know what could be causing that? my prof was saying something about double pointers, something about if i wanted to use one function to handle 2 lists... any clue?
A single = is used to SET the value and not compare the value.
Also you needed to update your curr pointer every time you add an emplist object. So the curr pointer is always pointing to the LAST item added like this:
You may need to examine the routine you use to print out the contents.
I changed the variable because in your main() function, you use the global curr variable.
That will corrupt your list because the global curr variable is holding important information (where to add new elements).
Here is how I printed the list:
1 2 3 4 5 6 7
for (emplist* e = head; e != NULL; e = e->nxt) //attempting to print list 1
{
cout << "inside for befor idqry" << endl;
e->idqry();
cout << "inside for after idqry" << endl;
}