Newbie C++ programmer again...

Rob Wehrli rwehrli@azpower.com
Fri, 18 Aug 2000 08:08:14 -0700


On Friday, August 18, 2000 7:57 AM, Robert Ambrose [SMTP:rna@testpt.com] 
wrote:
>
> What's happening with the 'cout >> tester;' is you are 'extracting' an 
int
> value from the input stream.  Because tester is an int, 'cout >> tester;'
> is trying to convert what you typed into an integer and put the result
> into tester.  If the conversion fails, i.e. you type non-numeric values 
as
> input, tester is not be changed and will contain whatever was in that
> stack location when the program was executed.
>
> The 'cout >> tester;' returns a true value if the conversion succeeds and
> a false value if it fails.
>
> Here's an example program:
>
> #include <iostream.h>
> #include <ctype.h>
>
> int main ()
> {
> 	int tester;
>
> 	cout << "enter some input:";
>
> 	if(cin >> tester)
> 		cout << "Numeric: " << tester << endl;
> 	else
> 		cout << "Not a numeric value" << endl;
>
> 	return 0;
> }
>
> rna

Not bad, however, it doesn't teach him how to use isalpha :)  Also, you 
should initialize "tester" before using it.  That way, you can spew it out 
to see if it was changed by anything that happened/didn't happen, too.  It 
will prevent more than just seg faults overtime...like help cure random 
debugging weirdness that "just seems to happen" to all of us more often 
than we'd prefer.

Take Care.

Rob!