Okay I think I have the main idea of whats goin on here, but when I run it there are multiple bugs. Note: this is not my code, I just want to discuss it.
Something is wrong with this code, I'm trying to debug it but I just can't figure out the problem. It compiles fine, but the problem happens when I enter a person's name with a space in it. It seems to run fine until I type in Bob Marley instead of BobMarley.Code:#include <string>
#include <iostream>
using namespace std;
struct node
{ char name[20]; // Name of up to 20 letters
int age; // D.O.B. would be better
float height; // In metres
node *nxt;// Pointer to next node
};
node *start_ptr = NULL;
node *current; // Used to move along the list
int option = 0;
void add_node_at_end()
{ node *temp, *temp2; // Temporary pointers
// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the name of the person: ";
cin >> temp->name;
cout << "Please enter the age of the person : ";
cin >> temp->age;
cout << "Please enter the height of the person : ";
cin >> temp->height;
temp->nxt = NULL;
// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}
void display_list()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "Name : " << temp->name << " ";
cout << "Age : " << temp->age << " ";
cout << "Height : " << temp->height;
if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;
}
cout << "End of list!" << endl;
}
}
void delete_start_node()
{ node *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}
void delete_end_node()
{ node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{ temp1 = start_ptr;
if (temp1->nxt == NULL)
{ delete temp1;
start_ptr = NULL;
}
else
{ while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}
void move_current_on ()
{ if (current->nxt == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->nxt;
}
void move_current_back ()
{ if (current == start_ptr)
cout << "You are at the start of the list" << endl;
else
{ node *previous; // Declare the pointer
previous = start_ptr;
while (previous->nxt != current)
{ previous = previous->nxt;
}
current = previous;
}
}
Then instead of running in a procedural manner it seems to just output all the remaining calls of add_node_at_end(); and skip the cin's.
Here is the main function
The first two add calls I keep everything kosher and without any spacesCode:int _tmain(int argc, _TCHAR* argv[])
{
srand ( unsigned(time(NULL)) );
start_ptr = NULL;
add_node_at_end();
add_node_at_end();
add_node_at_end();
add_node_at_end();
return 0;
}
The third call though I add one space, and look it fudges everythign up :d
Any ideas guys?Code:Please enter the name of the person: bobmarley
Please enter the age of the person : 40
Please enter the height of the person : 100
Please enter the name of the person: shamino55
Please enter the age of the person : 20
Please enter the height of the person : 100
Please enter the name of the person: hereisaspace !
Please enter the age of the person : Please enter the height of the person : Please enter the name o
f the person: Please enter the age of the person : Please enter the height of the person : Press any
key to continue . . .