// Linked list
Hi...
Below is the codes that I wrote so far...
everything is fine but just abit headache on when trying to read the data from the file...and it give me a weird things...

my output suppose is like this

==============================================================
 No                   Name/Site       Location     Weight
   01                ThunderStone         Russia        1250
   02                   Ramesseum          Egypt        1000
   03                   Trilithon        Lebanon         800
   04             ColossiOfMemnon          Egypt         700
   05             AlexanderColumn         Russia         600
   06                WesternStone         Israel         550
   07                  GreatStele       Ethiopia         520
==============================================================

but it given me this =,=||

================================================================
   No                   Name/Site       Location      Weight
   --                   ---------       --------      ------
   01                ThunderStone         Russia        1250
Egypt1000                        1000              nTrilitho:?
   04             ColossiOfMemnon          Egypt         700
Russi600                         600           toneWesternSU (
   07                  GreatStele       Ethiopia         520
India400                         400        Pyramid    Khafre's
   10             MasudaNoIwafune          Japan         300
Franc300                         300         Pillar    Pompey's
================================================================

I try my best to fix it already but still get the same thing...
anyone can give me a guide? thx...

#include<iostream>
#include<fstream>
#include<iomanip>
#define clrscr() system("cls");

using namespace std;

class stone	
{
friend class list;
  public:
	char no[5];			
	char name[20];			
	char location[10];		
	char weight[8];			

	stone()
	{
		next=0;
		for (int i=0;i<5;i++)	no[i]=0;
		for (int i=0;i<20;i++)	name[i]=0;
		for (int i=0;i<10;i++)	location[i]=0;
		for (int i=0;i<8;i++)	weight[i]=0;
	}
	
	void s();
	void setno(char no1[5])
	{
        for(int i=0;i<5;i++)
                no[i]=no1[i];
	}
	void setname(char name1[20])
	{
        for(int i=0;i<20;i++)
                name[i]=name1[i];
	}
	void setlocation(char location1[10])
	{
        for(int i=0;i<10;i++)
                location[i]=location1[i];
	}
	void setweight(char weight1[8])
	{
        for(int i=0;i<8;i++)
                weight[i]=weight1[i];
	}

	stone *next;	
	char * outno(){return no;};
	char * outname(){return name;};
	char * outlocation(){return location;};
	char * outweight(){return weight;};
};

void stone::s()		
{
	cout<<"Please Enter the Number: " << endl;
	cin>>no;
	cout<<"Please Enter the Name: " << endl;
	cin>>name;
	cout<<"Please Enter the Location: " << endl;
	cin>>location;
	cout<<"Please Enter the Weight: " << endl;
	cin>>weight;
}

class list		
{
  public:
	stone *last,*first;			
	list(){last=0;first=0;};
	~list();
	void instone();				
	void scanall();				
};

list::~list()					
{
	if(first!=0)
	{
		stone *p=first,*temp;
		while(p!=NULL){
		temp=p;
		p=p->next;
		delete temp;
		cout<<"ok\n";
		}
	}
}

void list::instone()
{ 
	stone *p=new stone();
	p->s();
	if(first==0){first=last=p;}else{last->next=p;last=p;}
}

void list::scanall()
{
	stone *temp;
	for(temp = first;temp!=NULL;temp=temp->next)
		cout<<setw(5)<<temp->outno()<<setw(28)<<temp->outname()<<setw(15)<<temp->outlocation()
					<<setw(12)<<temp->outweight() <<endl;
}

int main()
{
	char file[20],no[5],name[20],location[10], weight[8],temp[80];
	stone *p, *p1=new stone(),*q;
	ifstream input;		
	ofstream output;	
	int m=0,i=0;
	list will;			

	do{
		clrscr();
		cout<<"================================" << endl;
		cout<<" 1. For Adding" << endl;
		cout<<" 2. Display All the information" << endl;
		cout<<" 3. Build File to STORE" << endl;
		cout<<" 4. Open Specify File" << endl;
		cout<<"================================" << endl;
		cout<<endl<<"Choice:";
		cin>>m;
		switch(m)
		{
			case 1:
				will.instone();
			break;
			case 2:
				cout<<setw(5)<<"No"<<setw(28)<<"Name/Site"<<setw(15)<<"Location"<<setw(12)<<"Weight"<<endl;
				cout<<setw(5)<<"--"<<setw(28)<<"---------"<<setw(15)<<"--------"<<setw(12)<<"------"<<endl;
				will.scanall();
			break;
			case 3:
                cout<<"Please Enter the FILE NAME:"<<endl;
                cin>>file;
                output.open(file);
                p = will.first;
                output<<"List06"<<endl<<setw(5)<<"No"<<setw(28)<<"Name/Site"<<setw(15)
				<<"Location"<<setw(12)<<"Weight\n";
                for(;p!=NULL;p=p->next)
                        output<<setw(5)<<p->outno()<<setw(28)<<p->outname()<<setw(15)
                        <<p->outlocation()<<setw(12)<<p->outweight()<<endl;
                output.close();
			break;
			case 4:
                cout<<"Please Enter the File name that you wish to OPEN:"<<endl;
                cin>>file;
                input.open(file);
             
                input.getline(temp,80);       
                input.getline(temp,80);      
				
             
                input>>no>>name>>location>>weight;
                input>>temp;
				input>>temp;
                p1->setno(no);
                p1->setname(name);
                p1->setlocation(location);
                p1->setweight(weight);
                will.first=p1;
                will.last=will.first;

                while(!input.eof())
                {
					p=new stone();
					q=will.last;
					will.last->next=p;
					will.last=p;
					input>>no>>name>>location>>weight;
					will.last->setno(no);
					will.last->setname(name);
					will.last->setlocation(location);
					will.last->setweight(weight);
					input>>temp;
					input>>temp;
                }
                will.last=q;
                will.last->next=NULL;
                input.close();
			break;
		}
	cout <<endl;
	cout<<"Press 1: continue     Press 0: Exit" <<endl;
	cin>>m;
	}while(m==1);
}

I solve the problem by myself already

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.