bool perfectNumber(number);
This does not call the perfectNumber
function; it declares a local variable named perfectNumber
type bool
and initializes it with the value number
converted to type bool
.
To call the perfectNumber
function, you need to use something line by line:
bool result = perfectNumber(number);
or
bool result(perfectNumber(number));
In another note: if you are going to read input from a stream (for example, cin>>number
), you must make sure that retrieving the value from the stream succeeds. As now, if you typed asdf
, the extraction will fail and number
will remain uninitialized. The best way to verify the success of the extraction is to simply check the status of the stream:
if (cin >> number) { bool result = perfectNumber(number); } else { // input operation failed; handle the error as appropriate }
You can learn more about how the error conditions of the stream are set, and reset to Flags semantics on basic_ios
. You should also consult a good, introductory version of the C ++ book for more efficient flow control methods.
James McNellis
source share