Colossusx10
n00b
- Joined
- May 19, 2007
- Messages
- 8
I HAVE A NEW QUESTION!
How do I use the overloaded >> operator to print the result? I have no idea how to do it!
--------------------------------------------------------------------------------------------------------------------------------------
OK so I wrote this function which is supposed to add peoples names alphabetically. It is called InsertPassenger, and it calls on a function called Add (just to make it fancy). If you ask me, I think the codes works. But if you ask the compiler it'll say I'm wrong (apparently)
I also wrote a Print function to check if Add works. I'm not sure which one doesn't work, Add or Print. When I say doesn't work, I mean the following:
The program starts, I enter a name, and everything is fine. Then I choose to print and that's where I get an error. The program says it had to exit out. When I debugg, I'm told that there's a problem with this line within the Print function:
Could someone please tell me why this doesn't work:
First of all my structure:
These are the functions:
Any help would be greatly appreciated
How do I use the overloaded >> operator to print the result? I have no idea how to do it!
--------------------------------------------------------------------------------------------------------------------------------------
OK so I wrote this function which is supposed to add peoples names alphabetically. It is called InsertPassenger, and it calls on a function called Add (just to make it fancy). If you ask me, I think the codes works. But if you ask the compiler it'll say I'm wrong (apparently)
I also wrote a Print function to check if Add works. I'm not sure which one doesn't work, Add or Print. When I say doesn't work, I mean the following:
The program starts, I enter a name, and everything is fine. Then I choose to print and that's where I get an error. The program says it had to exit out. When I debugg, I'm told that there's a problem with this line within the Print function:
Code:
while ( current->next != NULL )
First of all my structure:
Code:
struct Air
{
string person;
Air *next;
};
These are the functions:
Code:
void JGOp::InsertPassenger()
{
cout << "Enter passenger: ";
cin >> input;
Add(input);
}
void JGOp::Add(string input)
{
Air *current, *previous;
Air *newp=new Air;
// populate newp
newp->person = input;
newp->next = NULL; // very important to initialize this
if (head == NULL)
{
head = newp;
return;
}
else if (newp->person < head->person)
{
newp->next=head;
head=newp;
return;
}
current = head;
while (current)
{
if (newp->person < current->person)
break;
previous = current;
current = current->next;
}
previous->next = newp;
newp->next = current;
return;
}
void JGOp::Print()
{
Air *current;
Air *head=new Air;
current = head;
if ( current != NULL )
{
while ( current->next != NULL )
{
cout << current->person;
current=current->next;
}
cout<< current->person;
}
}
Any help would be greatly appreciated