From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.1 required=5.0 tests=BAYES_00,HEADER_SPAM, PP_MIME_FAKE_ASCII_TEXT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,b30bd69fa8f63cb2 X-Google-Attributes: gid103376,public X-Google-Thread: fc772,b30bd69fa8f63cb2 X-Google-Attributes: gidfc772,public X-Google-ArrivalTime: 2003-06-15 11:28:55 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!xmission!news-out.spamkiller.net!propagator2-maxim!news-in-maxim.spamkiller.net!usc.edu!rpi!not-for-mail From: Francis Glassborow Newsgroups: comp.lang.ada,comp.lang.c++.moderated Subject: Re: C bug of the day Date: 15 Jun 2003 14:31:04 -0400 Organization: Southfield Microcomputer SS Sender: cppmods@netlab.cs.rpi.edu Message-ID: References: <1054751321.434656@master.nyc.kbcfp.com> Reply-To: Francis Glassborow NNTP-Posting-Host: netlab.cs.rpi.edu X-Original-Date: Sun, 15 Jun 2003 12:55:29 +0100 X-Submission-Address: c++-submit@netlab.cs.rpi.edu X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated iQBVAwUAPuy7ZEHMCo9UcraBAQFJNgH8DZ5FYmJ/bZ/NY3qerk0AArAvZRKyvgy/ fiZB9lgHybOgUQgdG87DMqazakLA0WECF66uS6ebgiAr/eQ6PX70EQ== =0aso Xref: archiver1.google.com comp.lang.ada:39205 comp.lang.c++.moderated:68402 Date: 2003-06-15T14:31:04-04:00 List-Id: In message , Terje Sletteb� writes >I would be interested to know how read() is implemented. Could you >posted it? Well for what they are worth here is the complete set of overloaded function templates Comments for improving them are very welcome. // These templates provide safe input for any type that supports operator>> // 1) from console with prompt template in_type read(std::string const & prompt, int max_tries = 3){ in_type temp; int tries(0); while(tries++ != max_tries ){ std::cout << prompt; std::cin >> temp; if(std::cin.good()) return temp; std::cin.clear(); // if it has failed, reset it to normal while(cin.get() != '\n'); flush stream std::cout << "\n That input was incorrect, try again: \n"; } throw fgw::problem("Too many attempts to read data."); } // 2) version without prompt, defaults to colon space prompt template in_type read(int max_tries=3){ return read(": ", max_tries); } // 3) version for general input stream, throws an exception if fails template in_type read(std::istream & in){ in_type temp; in >> temp; if(in.good()) return temp; if(in.eof()) throw fgw::problem("Tried to read past end of file"); // handle end of file as special case in.clear(); // reset input stream throw fgw::problem("Corrupted data stream."); } -- ACCU Spring Conference 2003 April 2-5 The Conference you should not have missed ACCU Spring Conference 2004 Late April Francis Glassborow ACCU [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]