Newbie C++ programmer again...

Rob Wehrli rwehrli@azpower.com
Fri, 18 Aug 2000 07:49:30 -0700


On Thursday, August 17, 2000 6:40 PM, Eric Samson 
[SMTP:airickjay@hotmail.com] wrote:
> Hello again all...
>
> I am trying to figure out the proper way to use the isalpha function, 
when I

All keyboard entry is going to come in as a char type.  A char is a single 
byte.  Changing your code to spew out the value in your int storage allows 
you to see what you're putting in it.  What you can derive from this is 
that your cin magician is saying...hummmm, not a int so I'm not gonna try 
to stuff a char type into an int type because that would produce weirdness 
beyond my means to comprehend...and since typeness is important to me, I'm 
keeping to it.  Also, always initialize variables before using them.

#include <iostream.h>
#include <ctype.h>

int main ()

{
  int tester = 0;

  cout << "Enter some input :" << endl;
  cin >> tester;

  if( isalpha (tester) )
    cout << "Alpha!" << endl;
  else
    cout << "Not Alpha!\tTester is " << tester << endl;
  return 0;
}

transformer:~/src/test$ g++ -o tester tester.cpp
transformer:~/src/test$ ./tester
Enter some input :
g
Not Alpha!      Tester is 0
transformer:~/src/test$ ./tester
Enter some input :
3
Not Alpha!      Tester is 3

*** note the following output when I change the initialization of "int 
tester" to be 7 ***
Enter some input :
g
Not Alpha!      Tester is 7

Now you can see that "cin" is saying...ain't me.  I'm getting a char and 
I'm expected to stuff it into an int and since a char isn't an int, I'm not 
sure why or how I should convert it, so bugger me, I'm outta here! 
 However, when it is an "int" in a char (remember that cin gets it as a 
char from stdin) hey man, this looks like an int and I'm asked to put it in 
an int so, cool, I can do that.  Here ya go duuuude.

Take Care.

Rob!

> enter a digit, it returns 0, and when I enter an alpha character, I get a 
> segmentation fault..  Can anyone tell me what I am doing wrong??
>
> Thanks in advance,
>
> Eric
>
> Program:
>
> #include <iostream.h>
> #include <ctype.h>
>
>
> int main ()
>
> {
>
> 	int flag;
> 	int tester;
>
>
> 	cout << "\n\nenter some input :";
>
>
>
> 		cin >> tester;
>
>
> 		flag = isalpha (tester);
>
>
>
> 		cout << "\n\n" << flag << "\n\n";
>
> 		return 0;
>
> 	}
>
> output:
> ---------------------
> alpha character
>
> $ ./test
>
>
> enter some input :g
> Segmentation fault
>