I've got the linked list working (it seems at least). Now I want to query the user to add another node to the list.
Here's the linked list so far:
Code:
struct node
{
string name;
string ID;
string address;
node *next;
};
int main(int argc, char *argv[])
{
string line;
char add_resp[3];
ifstream inFile ("A1.txt");
node *head = NULL;
for (int i = 0; i < 20; i++)
{
node *newPerson;
node *person_ptr;
newPerson = new node;
//Get name
getline(inFile, line);
newPerson->name = line;
//Get ID
getline(inFile, line);
newPerson->ID = line;
//Get address
getline(inFile, line);
newPerson->address = line;
newPerson->next = NULL;
if (!head)
head = newPerson;
else
{
person_ptr = head;
while (person_ptr->next)
person_ptr = person_ptr->next;
person_ptr->next = newPerson;
}
}
When I run my query and the user says 'yes' to adding a new person, the cout for entering the persons name shows up, but then skips straight to the cout for the persons ID, without any cin for the name. The cin comes for ID, then goes to cout for address, cin address, query, loop. It looks to me like they're all the same, so why is it skipping the name cin?
Code:
cout << "Would you like to add a person to this list? yes / no" << endl;
cin >> add_resp;
while (add_resp == "yes")
{
int keepgoing = 1;
while (keepgoing == 1)
{
node *newPerson;
node *person_ptr;
newPerson = new node;
//Get name
cout << "Enter the name for the person to be added formatted as last_name, first_name" << endl;
getline(cin, line);
newPerson->name = line;
//Get ID
cout << "Enter the ID number for " << newPerson->name << endl;
getline(cin, line);
newPerson->ID = line;
//Get address
cout << "Enter the entire address for the person to be added formatted as \"123 Main, Bigcity, SmallState Zipcode\"" << endl;
getline(cin, line);
newPerson->address = line;
newPerson->next = NULL;
if (!head)
head = newPerson;
else
{
person_ptr = head;
while (person_ptr->next)
person_ptr = person_ptr->next;
person_ptr->next = newPerson;
}
cout << "Would you like to add another person to this list?" << endl;
cin >> add_resp;
if (add_resp == "no")
keepgoing = 0;
}
}