isalpha function not working properly

I can't get the isalpha function from ctype library to work. I've tried importing <stdio.h> and <ctype.h>. If I run the isapha code shown below in another software that executes code (CodeRunner, Unix Executable, etc., it works fine. However, with secure shell in terminal, it completely ignores the function.

Am I forgetting to import or initialize something?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main ()
{

for (int i = 0; i < 5; i++){

    if (isalpha(c[i]) == true)  //c[i] is a character within array
    {   

       <body>;

    }
}


return 0;
}
Last edited on
It always works. You are doing something wrong.

Show us the code (or some example code) that actually exhibits the problem.
Okay, here it is. The goal of this program is to spell out each letter of an inputed string from the user to the ICAO words.

NOTE: The code below with the isalpha function works with CodeRunner, Unix Exec., and other compilers EXCEPT Secure Shell (./a.out) on the terminal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>

using namespace std;

int main ()
{
	string alphabet = "abcdefghijklmnopqrstuvwxyz";

        //ICAO words
	string strA = "Alfa";
	string strB = "Bravo";
	string strC = "Charlie";
	string strD = "Delta";
	string strE = "Echo";
	string strF = "Foxtrot";
	string strG = "Golf";
	string strH = "Hotel";
	string strI = "India";
	string strJ = "Juliet";
	string strK = "Kilo";
	string strL = "Lima";
	string strM = "Mike";
	string strN = "November";
	string strO = "Oscar";
	string strP = "Papa";
	string strQ = "Quebec";
	string strR = "Romeo";
	string strS = "Sierra";
	string strT = "Tango";
	string strU = "Uniform";
	string strV = "Victor";
	string strW = "Whiskey";
	string strX = "X-ray";
	string strY = "Yankee";
	string strZ = "Zulu";

        //Declaring character of each ICAO letter
	char cA = strA.at(0);
	char cB = strB.at(0);
	char cC = strC.at(0);
	char cD = strD.at(0);
	char cE = strE.at(0);
	char cF = strF.at(0);
	char cG = strG.at(0);
	char cH = strH.at(0);
	char cI = strI.at(0);
	char cJ = strJ.at(0);
	char cK = strK.at(0);
	char cL = strL.at(0);
	char cM = strM.at(0);
	char cN = strN.at(0);
	char cO = strO.at(0);
	char cP = strP.at(0);
	char cQ = strQ.at(0);
	char cR = strR.at(0);
	char cS = strS.at(0);
	char cT = strT.at(0);
	char cU = strU.at(0);
	char cV = strV.at(0);
	char cW = strW.at(0);
	char cX = strX.at(0);
	char cY = strY.at(0);
	char cZ = strZ.at(0);

        //Lower-case characters
	char cLowerA = alphabet.at(0);
	char cLowerB = alphabet.at(1);
	char cLowerC = alphabet.at(2);
	char cLowerD = alphabet.at(3);
	char cLowerE = alphabet.at(4);
	char cLowerF = alphabet.at(5);
	char cLowerG = alphabet.at(6);
	char cLowerH = alphabet.at(7);
	char cLowerI = alphabet.at(8);
	char cLowerJ = alphabet.at(9);
	char cLowerK = alphabet.at(10);
	char cLowerL = alphabet.at(11);
	char cLowerM = alphabet.at(12);
	char cLowerN = alphabet.at(13);
	char cLowerO = alphabet.at(14);
	char cLowerP = alphabet.at(15);
	char cLowerQ = alphabet.at(16);
	char cLowerR = alphabet.at(17);
	char cLowerS = alphabet.at(18);
	char cLowerT = alphabet.at(19);
	char cLowerU = alphabet.at(20);
	char cLowerV = alphabet.at(21);
	char cLowerW = alphabet.at(22);
	char cLowerX = alphabet.at(23);
	char cLowerY = alphabet.at(24);
	char cLowerZ = alphabet.at(25);

	cout << " " << endl;

	string inputStr;
	cout << "Enter a string to spell into a series of ICAO words:";
	cin >> inputStr;

	cout << " " << endl;

	int charCount = inputStr.length();
	cout << "The string: " << inputStr << " contains " << charCount << " characters" << endl;

	cout << " " << endl;

	char c[50];
	for (int i = 0; i < charCount; i++){
		c[i] = inputStr.at(i);
		cout << "The following character of the string is:" << c[i] << endl;
	}

	cout << " " << endl;

	for (int i = 0; i < charCount; i++){
		if (isalpha(c[i]) == true){     //isalpha function if statement not working
			if ( (c[i] == cA) || (c[i] == cLowerA) )
				cout << strA << " ";
			
			if ( (c[i] == cB) || (c[i] == cLowerB) )
				cout << strB << " ";
		
			if ( (c[i] == cC) || (c[i] == cLowerC) )
				cout << strC << " ";
	
			if ( (c[i] == cD) || (c[i] == cLowerD) )
				cout << strD << " "; 
		
			if ( (c[i] == cE) || (c[i] == cLowerE) )
				cout << strE << " "; 
		
			if ( (c[i] == cF) || (c[i] == cLowerF) )
				cout << strF << " "; 
		
			if ( (c[i] == cG) || (c[i] == cLowerG) )
				cout << strG << " "; 
		
			if ( (c[i] == cH) || (c[i] == cLowerH) )
				cout << strH << " "; 
		
			if ( (c[i] == cI) || (c[i] == cLowerI) )
				cout << strI << " "; 
		
			if ( (c[i] == cJ) || (c[i] == cLowerJ) )
				cout << strJ << " "; 
		
			if ( (c[i] == cK) || (c[i] == cLowerK) )
				cout << strK << " "; 
		
			if ( (c[i] == cL) || (c[i] == cLowerL) )
				cout << strL << " "; 
		
			if ( (c[i] == cM) || (c[i] == cLowerM) )
				cout << strM << " "; 
		
			if ( (c[i] == cN) || (c[i] == cLowerN) )
				cout << strN << " "; 

			if ( (c[i] == cO) || (c[i] == cLowerO) )
				cout << strO << " "; 
		
			if ( (c[i] == cP) || (c[i] == cLowerP) )
				cout << strP << " "; 
		
			if ( (c[i] == cQ) || (c[i] == cLowerQ) )
				cout << strQ << " "; 
		
			if ( (c[i] == cR) || (c[i] == cLowerR) )
				cout << strR << " "; 

			if ( (c[i] == cS) || (c[i] == cLowerS) )
				cout << strS << " "; 
	
			if ( (c[i] == cT) || (c[i] == cLowerT) )
				cout << strT << " "; 
		
			if ( (c[i] == cU) || (c[i] == cLowerU) )
				cout << strU << " "; 
		
			if ( (c[i] == cV) || (c[i] == cLowerV) )
				cout << strV << " "; 
		
			if ( (c[i] == cW) || (c[i] == cLowerW) )
				cout << strW << " "; 
		
			if ( (c[i] == cX) || (c[i] == cLowerX) )
				cout << strX << " "; 
		
			if ( (c[i] == cY) || (c[i] == cLowerY) )
				cout << strY << " "; 
		
			if ( (c[i] == cZ) || (c[i] == cLowerZ) )
				cout << strZ << " "; 
		}
		else
			cout << c[i] << " ";     //prints out this statement instead of isalpha

	}

	return 0;
}
Last edited on
Note that isalpha returns an integer.

When you compare a bool with an integer, true will be converted to 1 (and false to 0) so line 115
 
if (isalpha(c[i]) == true){
will do the same as
 
if (isalpha(c[i]) == 1){

isalpha will return zero if the character is not a letter. If the character is a letter it will return some other value, different from zero. It could return 1 but it could just as well return some other value.

To make it work correctly you should change the if statement to
 
if (isalpha(c[i]) != 0){
or simply
 
if (isalpha(c[i])){
Last edited on
I see.

So even though the position at c[i] is a character, the integer from i is still being compared with the Boolean.

I got the program to work. Thank you.

But.. then why did the other compilers ignore the bool and integer comparison?
Last edited on
So even though the position at c[i] is a character, the integer from i is still being compared with the Boolean.

No. It is the integer returned by isalpha that is being compared to the boolean.
By the way, it is not necessary to have all those extra variables. An 'A' is always an 'A', so you don't need a variable to use it. Just use it directly:

if ((c[i] == 'A') || (c[i] == 'a'))

You are making the same sort of mistake on line 106. Why do you need another variable for your inputStr? Just use the input string directly:

if ((inputStr[i] == 'A') || (inputStr[i] == 'a'))

You can shorten that with a function:

if (toupper(inputStr[i]) == 'A')

Since the alphabet is always ordered as A,B,C,..., you can also shorten your code considerably by putting the ICAO lookup into an array:

1
2
3
4
5
6
std::string ICAO_lookup[] = 
{
  "Alpha",
  "Bravo",
  ...
};

Then you can lookup a word very easily:

1
2
3
for (int i = 0; i < inputStr.length(); i++)
  if (isalpha(inputStr[i]))
    cout << ICAO_lookup[ toupper(inputStr[i]) - 'A' ] << " ";

Finally, using C++11's ranged-for loops, you can make that even easier on the eyes:

1
2
3
for (char c: inputStr)
  if (isalpha(c))
    cout << ICAO_lookup[ toupper(c) - 'A' ] << " ";

Hope this helps.
Wow, thank you Duoas!

The code I wrote in my second post was actually for a class. Our professor wanted us to be as "robust" as possible, and show that we understand how to store arrays, etc... An introductory crash course, go figure.

Anyhow, I value your feedback! The code you showed me on storing multiple strings into a single dimensional array is awesome! Will definitely use it for my other projects.

Thanks again.
:O)
Topic archived. No new replies allowed.