Thread: in need of help

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    in need of help

    hi all I need advice here I'm having a bit of a dilemma with this algorithm. We just started reading info from a text file check it out.
    Code:
    #include < iostream.h >
    #include < fstream.h >
    
    struct person
    {
    	 int id;
      float gpa;
      char name[19];
    };
    
    class myclass
    {
    private:fstream f;
    		person p;
    		
    public: void display1( )
    		{
    			char line[80]; fstream f;
    			f.open("data.txt" , ios::in);
    			while( true )
    			{
    			
    				f.getline(line, 19, '\n');
    				
    				f>>p.id>>p.gpa;
    				if(!f.fail())
    					break;
    			}
    			f.close();
    		}
    
    		void display2( )
    		{
    			f.open("data.txt", ios::in);
    
    			f>>p.id>>p.gpa;
    		
    		while(!f.eof())
    			{
    				 cout<<p.id<<"\t"<<p.gpa<<endl;
    				 f>>p.id>>p.gpa;
    			}
    		f.close();
    		}
    
    void display(char *fname)
    	{
    		f.open(fname, ios::in);
    		while(true)
    		{
    			f>>p.id>>p.gpa;
    			if(f.fail()) break;
    			cout<<p.id<<"\t"<<p.gpa<<endl;
    		}
    		f.close();
    		}
    };
    
    void main()
    {
    	myclass cs131;
    	cout<<"This is the content of the file\n";
    	cs131.display1();
    
    	cout<<"This is the content of the file\n";
    	cs131.display2();
    
    	cout<<"This is the content of the file\n";
    	cs131.display("data.txt");
    }
    Anyways I can't seem to get the name to print out no matter what I do anyone have any ideas

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I see:

    f>>p.id>>p.gpa;

    but I don't see where you read anything into p.name in display(), display1(), or display(2). In display1() you do read something into a local char array calle line:

    f.getline(line, 19, '\n');

    but then you do nothing with line once you have read something into line.
    Last edited by elad; 11-11-2004 at 03:43 PM.

Popular pages Recent additions subscribe to a feed