Thread View: alt.comp.lang.learn.c-c++
3 messages
3 total messages
Started by David Eynon
Thu, 30 Jan 2003 12:50
integer properties
Author: David Eynon
Date: Thu, 30 Jan 2003 12:50
Date: Thu, 30 Jan 2003 12:50
60 lines
2797 bytes
2797 bytes
//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
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Re: integer properties
Author: John Ericson
Date: Thu, 30 Jan 2003 21:19
Date: Thu, 30 Jan 2003 21:19
80 lines
3393 bytes
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! ]
Re: integer properties
Author: Francis Glassborow
Date: Thu, 30 Jan 2003 23:46
Date: Thu, 30 Jan 2003 23:46
87 lines
4506 bytes
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! ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads