Thread: operator '>>' reuse

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    operator '>>' reuse

    Planning to use the following to read from both file & user input..

    Code:
    std::istream& operator >> (std::istream& in, Contract& contract)
    {
    	std::string contractNo;
    	std::string rentalType;
    	unsigned lenderID;
    	unsigned carReg;
    	char carGrp;
    	Date iDate;
    
    	in >> contractID;
    	in >> carReg;
    	in >> carGrp;
    	in >> rentalType;
    	in >> iDate;
    
    	return in;
    }
    The in >> iDate works fine if reading from file, but problem is am not planning to read dates from user, rather generate them using current time ... This then forces me to never read date from any input, but i need to store date to be able to read it whenever i need to .. Should i rather have a separate '>>' just for date?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could, if you want, store your dates in a human readable format and making the >> operator read human readable format. That way you don't need a separate operator.
    But you can't exactly have two of the same operator.
    If the above solution is unsatisfying, how about a special class derived from the original date named something like HumanDate? You could overload the operators to read and write human readable dates and use it where appropriate.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My suggestion would be to plan to read dates from the user.

    If you are going to do "current time" transactions, then you would want a function MakeNewContract (or whatever you want to call it) that would get the contract details, get the current time into a Date thing, and call SetContractDetails from that function. (In other words: if you're creating a new contract using the current time, you're not reading in a "full" contract, which is what >> is meant to be.)

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You could, if you want, store your dates in a human readable format and making the >> operator read human readable format. That way you don't need a separate operator.
    Please show me how to do this!!! i thought about it but couldn't figure it!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by tabstop View Post
    My suggestion would be to plan to read dates from the user.

    If you are going to do "current time" transactions, then you would want a function MakeNewContract (or whatever you want to call it) that would get the contract details, get the current time into a Date thing, and call SetContractDetails from that function. (In other words: if you're creating a new contract using the current time, you're not reading in a "full" contract, which is what >> is meant to be.)
    Sorr i left something out of my code

    Code:
    std::istream& operator >> (std::istream& in, Contract& contract)
    {
    	std::string contractID;
    	std::string rentalType;
    	unsigned carReg;
    	char carGrp;
    	Date iDate;
    
    	in >> contractID;
    	in >> carReg;
    	in >> carGrp;
    	in >> rentalType;
    	in >> iDate;
    
    	contract.SetContractDetails(iDate, carReg, rentalType, contractID, carGrp);
    
    	return in;
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, it's not hard.
    If you have a date class, for output, it might be:

    Code:
    std::ostream& operator << (std::ostream& stream, const Date& outdate)
    {
        stream << std::setw(2) << outdate.year << " "
            << std::setw(2) << outdate.month << " "
            << std::setw(2) << outdate.day;
        return stream;
    }

    Code:
    const std::istream& operator >> (const std::istream& stream, Date& indate)
    {
        stream >> indate.year;
        stream >> indate.month;
        stream >> indate.day;
        return stream;
    }
    This assumes they're separated by " ", of course, but I'm pretty sure there are other methods to split with other characters, such as "-", although I'm not an expert on C++ I/O.
    These are only examples, not tested or compiled. They are from memory only.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Code:
    const std::istream& operator >> (const std::istream& stream, Date& indate)
    {
        stream >> indate.year;
        stream >> indate.month;
        stream >> indate.day;
        return stream;
    }
    This assumes they're separated by " ", of course, but I'm pretty sure there are other methods to split with other characters, such as "-", although I'm not an expert on C++ I/O.
    These are only examples, not tested or compiled. They are from memory only.
    I'm not sure if you understood me... I already did what you did here... Point is, i need to also generate indate from current time, not user input... In your example, am forced to always read from input , unless you suggesting i should have this function in addition to my other input one which doesn't read dates at all ... Then i will ONLY call this above function if i have to ...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, well, as you say - you want to use operator >>.
    Perhaps you're suggesting that current time is a different kind of object (ie not a stream)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Yes, well, as you say - you want to use operator >>.
    Perhaps you're suggesting that current time is a different kind of object (ie not a stream)?
    Yes, it's not a stream..

    Code:
    // Get the current system's time
    tm Date::getCurrentTime()
    {
    	time_t rawtime;
    	tm* timeinfo;
    
    	time( &rawtime );
    	timeinfo = localtime ( &rawtime );
    
    	return *timeinfo;
    }
    
    void Date::SetDate(tm curr_tm)
    {
    	day = curr_tm.tm_mday;
    	month = curr_tm.tm_mon;
    	year = curr_tm.tm_year;
    	hour = curr_tm.tm_hour;
    	minute = curr_tm.tm_min;
    	local_dt = curr_tm;
    }

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It might be probably easiest to create a separate function for console input. After all, you probably want prompt messages as well.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    It might be probably easiest to create a separate function for console input. After all, you probably want prompt messages as well.
    Lost you?

    Talking about this
    Code:
    	puts("\nENTER: First Name : Last Name : Driver's License : ID Number : Contact Number [ccc-ddd-dddd]\n");
    	
    	driver.Read(cin);
    
    	cin.ignore();
    
    	// Prompt for ContractID, carRegistration, group and kmType  
    	std::cout << "\nEnter new contract data below..." << std::endl;
    	std::cout << "1. Contract ID" << std::endl;
    	std::cout << "2. Car registration number" << std::endl;
    	std::cout << "3. Rental group [A/B/C/D/F/O/Z]" << std::endl;
    	std::cout << "4. Rental Type [Unlimited/200km per day]"
    				 "\n\n: --> Rental Type [Unlimited=\"unlimited\", 200km=\"200km\"]\n\n";
    	
    	cin.ignore();
    	
    	Contract contract;	
    
    	contract.Read(cin);	
    	
    	contracts.push_back(contract);
    
    	puts("\ncontract added...\n");

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by csonx_p View Post
    Lost you?

    Talking about this
    Code:
    	puts("\nENTER: First Name : Last Name : Driver's License : ID Number : Contact Number [ccc-ddd-dddd]\n");
    	
    	driver.Read(cin);
    
    	cin.ignore();
    
    	// Prompt for ContractID, carRegistration, group and kmType  
    	std::cout << "\nEnter new contract data below..." << std::endl;
    	std::cout << "1. Contract ID" << std::endl;
    	std::cout << "2. Car registration number" << std::endl;
    	std::cout << "3. Rental group [A/B/C/D/F/O/Z]" << std::endl;
    	std::cout << "4. Rental Type [Unlimited/200km per day]"
    				 "\n\n: --> Rental Type [Unlimited=\"unlimited\", 200km=\"200km\"]\n\n";
    	
    	cin.ignore();
    	
    	Contract contract;	
    
    	contract.Read(cin);	
    	
    	contracts.push_back(contract);
    
    	puts("\ncontract added...\n");
    Right. (This is the MakeNewContract-or-whatever function I suggested earlier.) I would read in after each prompt, rather than trying to get a whole bunch of stuff at the end. And then you do contract.date(SetDate(getCurrentTime())) and you're done. (Edit: Or do I mean contract.date.SetDate(getCurrentTime())? I got lost a little bit there, I think.)

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, since you ARE reading dates from a STREAM all the time, you can make sure that all dates are the same, everywhere across the program.
    Read dates in format XXXX-XX-XX, write dates in XXXX-XX-XX, Both from/to console input/output and from/to files.
    That way there's only one operator. Isn't that what you want? It's the impression I get.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Since you do different things depending on the stream the data is from though, I wouldn't give contracts a stream extractor (operator>>) at all.

    It seems more clear to simply create some free functions that work on contract: one that creates a contract from a file and another for user-entered contracts. That way, you can keep a stream extractor for *date* and just use it when it's appropriate to do so.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by tabstop View Post
    Right. (This is the MakeNewContract-or-whatever function I suggested earlier.) I would read in after each prompt, rather than trying to get a whole bunch of stuff at the end. And then you do contract.date(SetDate(getCurrentTime())) and you're done. (Edit: Or do I mean contract.date.SetDate(getCurrentTime())? I got lost a little bit there, I think.)
    prompt for each in this function?

    Code:
    std::istream& operator >> (std::istream& in, Contract& contract)
    {
    	std::string contractID;
    	std::string rentalType;
    	unsigned carReg;
    	char carGrp;
    	Date iDate;
    
    	in >> contractID;
    	in >> carReg;
    	in >> carGrp;
    	in >> rentalType;
    	in >> iDate;
    
    	contract.SetContractDetails(iDate, carReg, rentalType, contractID, carGrp);
    
    	return in;
    }
    Well, i guess then if i do that definitely need two separate functions for readin from user and file ... Which i was trying to avoid.. I prefer a code which can be reused ... Read() should cater for both keyboard & file ... If fact, if i do this indata >> Contract, how would it know if i'm calling the keyboard inout rather than the file input? will it decided based on the indata stream type?

    Code:
    ifstream indata;
    istream indata;
    
    istream& operator >> (istream& indata, Contract& contract)
    {
       ...
    }
    how would this function decide between the two streams?



    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what design patter let me reuse this class?
    By patiobarbecue in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2009, 01:44 PM
  2. reuse a socket connection
    By radeberger in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-18-2009, 11:38 AM
  3. Deallocate Memory and reuse pointer
    By appleGuy in forum C++ Programming
    Replies: 9
    Last Post: 06-20-2007, 11:07 AM
  4. homework v.s. reuse
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 12-08-2003, 02:18 PM
  5. exceptions: reuse <stdexcept> classes???
    By Ruchikar in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2002, 03:09 AM