🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #678

Re: integer properties

#678
From: Francis Glassborow
Date: Thu, 30 Jan 2003 23:46
87 lines
4506 bytes
In message <wk0_9.474$Pe7.97@nwrddc04.gnilink.net>, David Eynon
<david.eynon@verizon.net> writes
>//NOTES: When account number is entered as a large number in the beginning,
>the program
>// terminates. When account number is entered as a large number in the
>middle
>//  an infinite loop occurs. Why is this?

I suspect that the account number you are entering is causing the
input stream to fail. It happens on your system that an uninitialised
int is starting at zero so when your program first checks acct it is not
greater than zero and so the loop is never entered and the program just
terminates.

However once it gets into the loop, acct has a current value that is
greater than zero. Now when cin fails that value is never altered and so
the loop never terminates.

If you explicitly store 0 in acct before attempting to read in a new
value I think you will find your program consistently terminates for
large enough input values. I am not sure that it should, but that is a
different issue.

Try checking the status of an input stream after each use. The following
may be crude but it will give you some ideas about safer input:

// These templates provide safe input for any type that supports
operator>>
// 1) from console without prompt

template<typename in_type>
        in_type read(std::string& const prompt){
                in_type temp;
                bool repeat(true);
                do {
                        std::cout << prompt;
                        std::cin >> temp;
                        if(!std::cin){  //test cin
                                std::cin.clear();       // if it has
failed, reset it to normal
                                clear_input_line(cin);
                                std::cout << "\n That input was
incorrect, try again: \n";
                        }
                        else repeat = false;
                }while (repeat);
                return temp;
        }

// 2) version without prompt, defaults to colon space prompt
template<typename in_type>
        intype read(){
                return read<intype>(": ");
        }

// 3) version for general input stream, throws an exception if fails
template<typename in_type>
        intype read(std::ostream in){
                in_type temp;
                in >> temp;
                if(!in){        est input worked
                        in.clear();  // reset input stream
                        std::cerr << "Corrupted data stream accessed in
read input template.\n";
                        throw tools::problem("Corrupted data stream.");
                }
                else return temp;
        }

They are used like this:

int i(read<int>());   //initialise from keyboard
double d(read<double>("Type in a double."));
ifstream in(data.txt);
d = read<double>(in);

I hope that helps.

-- 
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow      ACCU


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
Technical Details
Message-ID: <nVAoDazfJVO+EwFK@robinton.demon.co.uk>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!uwm.edu!rpi!not-for-mail
References: <wk0_9.474$Pe7.97@nwrddc04.gnilink.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,1e14887de54a4921
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,1e14887de54a4921
X-Google-Attributes: gid114592,public
X-Google-Thread: fc772,1e14887de54a4921
X-Google-Attributes: gidfc772,public
X-Google-Thread: 10b7bb,1e14887de54a4921
X-Google-Attributes: gid10b7bb,public
X-Google-ArrivalTime: 2003-01-30 15:44:48 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!uwm.edu!rpi!not-for-mail
From: Francis Glassborow <francis.glassborow@ntlworld.com>
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c++,comp.lang.c++.moderated,ctdl.lang.c++
Subject: Re: integer properties
Date: 30 Jan 2003 18:46:32 -0500
Organization: Southfield Microcomputer SS
Lines: 87
Sender: cppmods@netlab.cs.rpi.edu
Approved: jep@[151.161.11.6]
Message-ID: <nVAoDazfJVO+EwFK@robinton.demon.co.uk>
References: <wk0_9.474$Pe7.97@nwrddc04.gnilink.net>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Thu, 30 Jan 2003 16:27:11 +0000
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
 iQBVAwUAPjm5UkHMCo9UcraBAQE4HgIAoCBszq3DoxfqEXBDlTeoxvvd8GRzODtd
 93M1ChSQXcRrqT787/KM81jOK/MY3WTEd/GNVR7Si18uHAar4z9yUQ==
 =1Waa