🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: integer properties

#677
From: John Ericson
Date: Thu, 30 Jan 2003 21:19
80 lines
3393 bytes
"David Eynon" <david.eynon@verizon.net> wrote in message
news:wk0_9.474$Pe7.97@nwrddc04.gnilink.net...
> //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?
>
> #include <iostream>
>
> using std::cout;
> using std::cin;
> using std::endl;
>
> int main()
> {
>   int acct; //account number
>   double begbal = 0;
>   double tcharges = 0; //total charges
>   double tcredits = 0; //total credits
>   double limit = 0;  //credit limit.
>
>   cout << "Enter account number (-1 to stop)";
>   cin >> acct;
>
>   while (acct > 0)
>   {
>    cout << "Enter beginning balance:";
>    cin >> begbal;
>
>    cout << "Enter total charges:";
>    cin >> tcharges;
>
>    cout << "Enter total credits:";
>    cin >> tcredits;
>
>    cout << "Enter your credit limit:";
>    cin >> limit;
>
>    if ((begbal + tcharges - tcredits) > limit)
>    {
>     cout << "Account number: " << acct << endl;
>     cout << "Balance: " << (begbal + tcharges - tcredits) << endl;
>     cout << "Credit limit of " << limit << " has been exceeded"<<
endl;
>     //cout << endl;
>    } // end if
>
>    //next "Cycle"
>    cout << endl;
>    cout << "Enter account number (-1 to stop)";
>    cin >> acct;
>   } //while
>
>
>    return 0;
>
> } //main

You're not initializing 'acct' before use, and you're not checking for
error
conditions (for example, if you entered a thousands separator comma in
the
entered number).

For clearing an erroneous entry,
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3 (a
relatively recent addition to the FAQ), if( !(cin >> yourVariable) )
    std::cout << "That's not a number; ";
     std::cin.clear();
     std::cin.ignore(std::numeric_limits<int>::max(), '\n');

Best Regards, John E.



      [ 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: <jtb_9.12$Io2.8812743@newssvr15.news.prodigy.com>
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: 10b7bb,1e14887de54a4921
X-Google-Attributes: gid10b7bb,public
X-Google-Thread: 109fba,1e14887de54a4921
X-Google-Attributes: gid109fba,public
X-Google-Thread: fc772,1e14887de54a4921
X-Google-Attributes: gidfc772,public
X-Google-Thread: 114592,1e14887de54a4921
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2003-01-30 13:17:36 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: "John Ericson" <jericson@pacbell.net>
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 16:19:20 -0500
Organization: Prodigy Internet http://www.prodigy.com
Lines: 80
Sender: cppmods@netlab.cs.rpi.edu
Approved: hsutter@gotw.ca
Message-ID: <jtb_9.12$Io2.8812743@newssvr15.news.prodigy.com>
References: <wk0_9.474$Pe7.97@nwrddc04.gnilink.net>
NNTP-Posting-Host: netlab.cs.rpi.edu
X-Original-Date: Thu, 30 Jan 2003 15:25:03 GMT
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
 iQBVAwUAPjmW00HMCo9UcraBAQHfwwIAk8qPnbqniTApOxKWjq4OtuD36P7o6Jaa
 Pr1BXixW/IATmjSNl/zHA2pYjS1IJmsQL0XxU4JNAHGFJgjzcAsnhg==
 =rzf0