Question

This is a discussion on Question within the C++ Programming forums, part of the General Programming Boards category; ok im trying to read in data from a file with this info Ray L. Jones 3942 6 4 1974 ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    43

    Question

    ok im trying to read in data from a file with this info

    Ray L. Jones 3942 6 4 1974 3 2 2001

    and i have a class like this
    Code:
    class Instructor {
    
    public:
    	Instructor();   // constructor
    	~Instructor();	
    
    private:
    	string firstName;
    	string lastName;
    	string middleInitial;
    	string employeeID;
    	
    
    }; // end class Instuctor
    and my code in main looks like this. The line that says
    inFile >> *(I[i_index]);
    is not executing , it gives me an error and i think i would need to use operator overloading on a function for that line to be valid but im not sure.
    Code:
    Instructor *I[MAX_INSTRUCTORS] = {NULL_POINTER};
    
    	for(int i_index = LOOP_BASE; i_index < MAX_INSTRUCTORS; i_index++)
    	{
    		I[i_index] = new Instructor();
    		inFile >> *(I[i_index]); 
    		//What must I do to my class Instructor to make the line of code above work?
    		//cout << "The new instructor is " << *(I[i_index]) << ".\n";
       }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Try
    inFile.read(reinterpret_cast<char *>(I[i_index]) sizeof(Instructor));

    Although I think you're going to need to use a binary file with NULL seperating your strings.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    I can't do that i really need to make that particular line of code work, does anyone have any ideas.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I used this for my string class. You can easily change it to make it work for your Instructor class.
    Code:
    //In your .h file
    friend	ostream&	operator<<(ostream&,const String&);
    friend	istream&	operator>>(istream&,String&);
    
    //In your .cpp file
    ostream& operator<<(ostream& o, const String& newStr)
    {
    	return o << newStr.lpzstr;
    }
    
    istream& operator>>(istream& i,String& obj)
    {
    	char buffer[1024];
    	i.getline(buffer,1023,'\n');
    	delete [] obj.lpzstr;
    	obj.lpzstr = new char[strlen(buffer)+1];
    	strcpy(obj.lpzstr, buffer);
    	return i;
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    I see your code, but i don't see how that code would make this a vaild line of code
    Code:
    inFile >> *(I[i_index]);
    How will that read from the file?

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Hmm.... You were supposed to figure it out.

    What, exactly, must happen when you do the following :
    Code:
    inFile >> *(I[i_index]);

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    its supposed to read in data from a file that looks like this

    Ray L. Jones 3942 6 4 1974 3 2 2001
    Alice K. Brown 8300 4 8 1969 2 1 1999

    and store into a pointer of class Instructor which i have defined above and the line below the one that looks like this
    Code:
    inFile >> *(I[i_index]);
    then the line that prints the code looks like this
    Code:
    cout << "The new instructor is " << *(I[i_index]) << ".\n";
    which will print out something like this:
    The new instructor is Ray L. Jones 3942

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Why not add a function to your class to output the data and call that?
    Truth is a malleable commodity - Dick Cheney

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    because i can't do that, i have to make it fit what is already in main.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    ok i have overloaded the >> operator and in main i have a line of code like this Instructor *S[3] which is an array of 3 pointers to class Instructor and then also in main is the line:
    inFile >> *(I[i_index]);

    now the function that matches this is in my class Instructor which looks like this:
    Code:
    	friend istream &operator>>(ifstream &, Instructor *);
    
    	
    
    	
    
    private:
    	string firstName;
    	string lastName;
    	string middleInitial;
    	string employeeID;
    but in my definition of the function it says firstName, lastName and so on are undeclared
    Code:
    stream &operator>>(ifstream &I, Instructor *S)
    {
    	I>>firstName>>middleInitial>>lastName>>employeeID;
    }

  11. #11
    push
    Guest
    I'm doubting that you wrote this code yourself, or you're extremely sleepy. Think about it...
    Code:
    const ifstream &operator>>(ifstream &I, Instructor *S)
    {
        I >> S->firstName >> S->middleInitial >> S->lastName 
          >> S->employeeId;
    
        return I;
    }

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    This code is in main
    Code:
    for(int i_index = LOOP_BASE; i_index < MAX_INSTRUCTORS; i_index++)
    	{
    		I[i_index] = new Instructor();
    		inFile >> *(I[i_index]);
    This is in class Instructor
    Code:
    class Instructor {
    
    public:
    	Instructor();   // constructor
    	~Instructor();
    	friend istream &operator>>(ifstream &I, Instructor *S);
    and this is in the member functions file
    Code:
    ifstream &operator>>(ifstream &I, Instructor *S)
    {
        I >> S->firstName >> S->middleInitial >> S->lastName 
          >> S->employeeID;
    
    }
    it gives me this error
    Compiling...
    Driver.cpp
    C:\Documents and Settings\Massa\Driver.cpp(91) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class Instructor' (or there is no acceptable conversion)
    Error executing cl.exe.

    Driver.exe - 1 error(s), 0 warning(s)

  13. #13
    push
    Guest
    Again wake up...and think a little...

    Code:
    inFile >> *(I[i_index]);
    in the above line you're passing an Instructor object to the operator NOT its address. You either need to change it, or change the operator definition...

    Code:
    const ifstream &operator>>(ifstream &I, Instructor &S)
    {
        I >> S.firstName >> S.middleInitial >> S.lastName 
          >> S.employeeID;
    }

  14. #14
    push
    Guest
    dont forget to return the ifstream reference in the defintion...

  15. #15
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Nice thread-topic btw.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21