Re: [Dev-C++] while termination problems
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-10-06 08:17:46
|
Also, you should _not_ compare floating point numbers with == and != since
floating point numbers - even if looking ok in a printout - are often
inexact, i.e. have a bit of noise in the last value digits. If you think a
variable has the value -1, it might just as well have the value
-1.0000000001 which obviously would make your compare to fail.
Your test should in this case instead have been
if (acctnum < -0.5) break;
which would break for -1 while not breaking for 0.
Normally the general floating point compare for equality would look like:
if (fabs(acctnum - -1) < EPS) break;
where EPS is a suitably small number. Note that EPS must be larger for
float variables than for double variables. Also note that EPS must be
larger, the larger numbers that are compared.
/Per W
On Tue, 5 Oct 2004, Reid Thompson wrote:
> as a note, you should avoid using C and C++ headers in the same program,
> use cin.get rather than system("PAUSE") to keep your window around. You
> should also initialize your variables before using them.
> not sure if this is what you mean,,,... but....
>
> #include <iostream>
> #include <iomanip>
>
> using namespace std;
>
> int
> main(int argc, char *argv[])
> {
> /* arbitrarily initializing all of these to 0 */
> double acctnum = 0;
> double bBal = 0;
> double nBal = 0;
> double charges = 0;
> double limit = 0;
> double credits = 0;
>
> while (acctnum != -1)
> {
>
> cout << "Enter account number (enter -1 to end): ";
> cin >> acctnum;
> if (acctnum == -1)
> {
> break; /* break the while loop */
> }
>
> cout << "Enter beginning balance: ";
> cin >> bBal;
>
> cout << "Enter total charges: ";
> cin >> charges;
>
> cout << "Enter total credits: ";
> cin >> credits;
>
> cout << "Enter credit limit: ";
> cin >> limit;
>
> nBal = bBal + charges - credits;
>
> cout << "Account:" << setw(13) << acctnum << "\n";
> cout << "Credit Limit:" << setw(9) << limit << "\n";
> cout << "Balance:" << setw(14) << nBal << "\n";
>
> if (nBal > limit)
> cout << "Credit Limit Exceeded.\n";
> }
>
> cin.ignore();
> cin.get();
> return 0;
> }
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
> Use IT products in your business? Tell us what you think of them. Give us
> Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
> http://productguide.itmanagersjournal.com/guidepromo.tmpl
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|