I am having a problem with the printing of a linked list. If I enter two records
ID: 3636024 • Letter: I
Question
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 record printout routine and its use of the traverse variable seem to be the problem. I took out the while an am using a for loop. This fixes the issue but of course I can no know how many records there are. /code
#include <iostream>
// #include <string>
#include <sstream>
// #include <stdio.h>
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 " <<traverse->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 ttwo 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
Hi I have done the solution for this in my notebook. But the solution is too big , I cannot type it here because very less time is remaining.. So please rate me Lifesaver and I'll share the answer with you through email or cramster inbox. I don't do this generally, but I have no other option here because there's very less time left... you need not worry as I have the solution ready
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.