Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using CodeBlocks IDE. I am having a problem with the printing of a linked list.

ID: 3636067 • Letter: U

Question

Using CodeBlocks IDE.
I am having a problem with the printing of a linked list. If I enter two records worth of values by using 8 ones for the first record and 8 twos for the second record I get a few lines to unwanted numbers after I run a routine to print out the two records.

The desired effect is to remove the for (int i=1;i<3;i++) { line and use the while line after. This is when the trouble starts

The record printout routine and its use of the traverse variable seem to be the problem. I took out the while and am using a for loop. This fixes the issue but of course I can not know how many records there are. I need to fix this printout loop

#include
// #include
#include
// #include
using namespace std;

struct node
{
string fname; // first name
string lname; // last name
string address1; // number and street
string address2; // address, city state zip combined up to 20 characters
int birthmonth; // birth month integer will need to be converter via switch statement for prin out.
int birthday; // Birthday integer
int anniversarymonth; // Anniversary month
int anniversaryday; //Anniversary day
node *next; //pointer to next node
};

int main()

{
char answer1;
answer1 = 'n';
string st1;
node *root;
node *traverse;
root = new node;
root->next = NULL;
traverse=root;
while (!( answer1 == 'y' )) // routine to do add records
{
cout << "Please enter the following information ";
cout <<"First name of the person: ";
cin >> ws;
getline (cin, traverse->fname);
cout << "Last name of the person: ";
cin >>ws;
getline (cin, traverse->lname);
cout << "The number and street of the person: ";
cin >> ws; // white space handler
getline (cin,traverse->address1);
cout << "The city, State and zip for the person: ";
cin >>ws; // Whitespace handler address wil be intact
getline (cin,traverse->address2);
cout << "The birthday month as a number: ";
cin >> traverse->birthmonth;
cout << "The birthday day as a number: ";
cin >> traverse->birthday;
cout << "The Anniversary month as a number: ";
cin >> traverse->anniversarymonth;
cout << "The annversary day as a number: ";
cin >> traverse->anniversaryday;
cout <<" hello " <anniversaryday << " ";
traverse->next = new node;
traverse=traverse->next;
cout <<"Are you finished entering values to the list? (y or n): ";
cin >> answer1;
}
cout << "done entering data into list ";

// //////////// printout subprogram
traverse = root;

for (int i=1;i<3;i++) {
// I want to use some version of the next two lines instead since the number of records is to be variable
// while(traverse->next !=NULL) {
cout << traverse->fname << endl;
cout << traverse->lname << endl;
cout << traverse->address1 << endl;
cout << traverse->address2 << endl;
cout << traverse->birthmonth << endl;
cout << traverse->birthday << endl;
cout << traverse->anniversarymonth << endl;
cout << traverse->anniversaryday << endl;
cout<< endl;
traverse = traverse->next;
}
/* cout << traverse->fname << endl;
cout << traverse->lname << endl;
cout << traverse->address1 << endl;
cout << traverse->address2 << endl;
cout << traverse->birthmonth << endl;
cout << traverse->birthday << endl;
cout << traverse->anniversarymonth << endl;
cout << traverse->anniversaryday << endl;
cout<< endl;
*/

}

Explanation / Answer

Please tell me what error occured