Newbie C++ programmer again...

der.hans PLUG@LuftHans.com
Thu, 17 Aug 2000 19:43:49 -0700 (MST)


Am 17. Aug, 2000 schwäzte Eric Samson so:

> Hello again all...
> 
> I am trying to figure out the proper way to use the isalpha function, when I 
> 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??

You're assigning your input to an int, so it can't be alpha.

sh-2.01$ cat fred.cc
#include <iostream.h>
#include <ctype.h>

int main () {
    int flag;
    char tester;

    cout << "enter some input: ";
    cin >> tester;
    flag = isalpha (tester);
    cout << "char tester <" << tester << ">,\tint flag <" << flag <<
">\n";
    return 0;
}
sh-2.01$ make fred
g++     fred.cc   -o fred
sh-2.01$ ./fred 
enter some input: k
char tester <k>,        int flag <1024>
sh-2.01$ ./fred 
enter some input: 2
char tester <2>,        int flag <0>
sh-2.01$ ./fred 
enter some input: .
char tester <.>,        int flag <0>
sh-2.01$ 

I don't know how you were getting the segfault. I didn't get one, but I
was getting the wrong results. Maybe your compiler is more anal about
assigning a char to an int.

Also, the "make fred" trick is pretty cool. Make has a bunch of internal
rules, so it can automagically turn fred.cc into fred with g++ or fred.c
into fred with gcc.

As you start working more with compiled languages you'll be wanting to
know make :).

ciao,

der.hans
-- 
#  der.hans@LuftHans.com   home.pages.de/~lufthans/   www.Opnix.com
# HERE LIES LESTER MOORE
# SHOT 4 TIMES WITH A .44
# NO LES
# NO MOORE
#        -- tombstone, in Tombstone, AZ