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?
#include <iostream>
usingnamespace 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;
}
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
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:
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.