Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.

The text file just has numbers like this:
12
34
56
34
67
23
64
23

#include <iostream>
#include <fstream>

using namespace std;

class List
{
public:
	void Insert(int);
	void Print();
};

struct node
{
	int age;
	node *nxt;
}; 
node *start_ptr = 0;
node *current;

void List::Insert(int age)
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node; 
	 temp->age;
    
     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 List::Print()
  {  
	 node *temp;
     temp = start_ptr;
     
     if (temp == NULL)
       cout << "The list is empty" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout << "Age : " << temp->age << " ";
	   
	      if (temp == current)
		cout << " <-- Current node";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }
void main()
  {  
	  start_ptr = NULL;
	  List display;
	  fstream inFile;
	  node number;
	  inFile.open("int.txt");
while(inFile>>number.age)
{
	display.Insert(number.age);
}
	display.Print();
  }

Recommended Answers

All 2 Replies

Im just getting garbage in the output. It outputs the correct number of items but its not the right numbers. Im not sure why it is happening.

The text file just has numbers like this:
12
34
56
34
67
23
64
23

#include <iostream>
#include <fstream>

using namespace std;

class List
{
public:
	void Insert(int);
	void Print();
};

struct node
{
	int age;
	node *nxt;
}; 
node *start_ptr = 0;
node *current;

void List::Insert(int age)
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node; 
	 temp->age;
    
     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 List::Print()
  {  
	 node *temp;
     temp = start_ptr;
     
     if (temp == NULL)
       cout << "The list is empty" << endl;
     else
       { while (temp != NULL)
	   {  // Display details for what temp points to
              cout << "Age : " << temp->age << " ";
	   
	      if (temp == current)
		cout << " <-- Current node";
              cout << endl;
	      temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
       }
  }
void main()
  {  
	  start_ptr = NULL;
	  List display;
	  fstream inFile;
	  node number;
	  inFile.open("int.txt");
while(inFile>>number.age)
{
	display.Insert(number.age);
}
	display.Print();
  }

These should really be data members of your List class, not global variables:

node *start_ptr;
node *current;

This line does nothing:

temp->age;

I assume you meant:

temp->age = age;

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.