Thread: overloading >>

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    overloading >>

    ok. My working code is below. what I now need to do is read from a file, which needs to be done with the overloaded>>. each field I read needs to be assigned to a variable listed in my class. I will also include my data file. Some general hints would be useful, as I have never overloaded a variable before in this way. Thanks for any input!

    Datafile:
    Smith|Jane|2105 E. J Avenue|Nowhere|Michigan|33333|H|1030|80|2

    Code:
    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cstdlib>
    using namespace std;
    
    class employee
    {
    public:
      string lname;
      string fname;
      string address;
      string city;
      string state;
      string zip;
      char hos;
      int payrate;
      int hours;
      int dependents;
      int gpay;
      int fed;
      int state1;
      int social;
      int net;
      int compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
    };
    employee::employee();
    int main()
    {
      char in[20];
      char out[20];
      char str[1024], last;
      cout<<"Enter the name of the file you want to write to."<<endl;
      cin.get(out, 20);
      cin.ignore(80, '\n');
      cout<<"Enter the name of the file you want to read from."<<endl;
      cin.get(in, 20);
      cin.ignore(80, '\n');
      ifstream infile;
      infile.open(in);
      ofstream outfile;
      outfile.open(out);
    
    
    
    	/*while(!infile.eof())
    	{
    		infile.get(str, 1023, '|');
    		cout<<str<<endl;
    		infile.get(last);
    	}*/
    
    	employee::lname=infile.get>>
      cout<<employee::lname;
    
    
    
    
    
      employee compute(int payrate, int hours, int dependents, int gpay, int fed, int state1, int social, int net, char hos);
      
      
      infile.close();
      outfile.close();
      return 0;
    }
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    disregard the employee::lname=infile.get>>

    cout<<employee::lname;

    I meant to comment those out
    This has been a public service announcement from GOD.

    111 1111

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    ifstream &operator>>(ifstream& stream, Person& p) {
       // the ifstream input would be something like this.
       // Jack|Campbel|bla..bla...bla...
       char tmp[80];
     
       stream.getline(tmp, 80, '|');
       p.setFirstName(tmp);
    
       stream.getline(tmp, 80, '|');
       p.setLastName(tmp);
    
      return stream;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    ok, I put that into my code, but now I am getting a this error:
    (63) : error C2601: '>>' : local function definitions are illegal
    It starts at the open bracket for the code you gave me, how do I remedy this?
    This has been a public service announcement from GOD.

    111 1111

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I hope you're not pasting my code inside your main(). Treat it as you would your compute(), meaning declare and define in where it belongs. In your main(), you will have something like this.
    Code:
    Person person;
    ...
    infile >> person;
    If you don't know how to add the details, then you got some homework to do on your own. It seems that you need some review on creating a class.
    Last edited by alphaoide; 12-05-2003 at 07:22 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading >>
    By h3ro in forum C++ Programming
    Replies: 21
    Last Post: 05-07-2008, 01:14 PM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. overloading >> operator
    By Diamonds in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2003, 02:01 AM
  5. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 10:56 PM