Help with ICAO code

Tndrmr
Tndrmr used Ask the Experts™
on
The attached code  uses an array to take a word  or group of letters and output the corresponding ICAO code but I need to add code so that if non-alphabetic characters are entered it will result in an out-of-bounds access. Would appreciate any help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
I hate to state that, but there is no "attached code" in your post ;o)

Author

Commented:
Not sure what happened but will try again. sorry
ICOA.cpp
jkr
Top Expert 2012

Commented:
You can add a simple check to skip non-aphabetic input using '_isalpha()', e.g.

#include <iostream>
#include <string>
#include <ctype.h>
 
using namespace std;
 
int main()
{
	
	//declare global variables
	int index = 0;
	string word;
 
	//input from user
	cout << "Input a word to return in ICAO form: "; 
	cin >> word;
	cout << endl;
 
	//the array, initialized with ICAO alphabet
	string ICAO[26] = {"Alpha", "Bravo", "Charlie", "Delta" , "Echo", "Foxtrot" , "Golf" , 
                      "Hotel" , "India" , "Juliet", "Kilo" , "Lima", "Mike" , "November" , 
                      "Oscar" , "Papa" , "Quebec", "Romeo" , "Sierra" , "Tango" , "Uniform" ,
                      "Victor" , "Whiskey" , "X-ray" , "Yankee" , "Zulu"};
 
	//loop while index number is less than the length of the input word
	while (index < (int)word.length())
	{
		//local variables
		string output;
		char letter;
		
		letter = word[index];			//get individual letter of input word
                if (!_isalpha(letter)) continue; // <------- skip non-alphabetic input
		letter = toupper(letter);		//convert to uppercase letter 						
		output = ICAO[letter - 'A'];	//display proper string from array
		cout << output << " ";			//output will be on one line
		index++;						//increment the index
	}
 
cout << endl;
 
 
system("pause");
return 0;
}

Open in new window

Top Expert 2012
Commented:
Sorry, correction:

#include <iostream>
#include <string>
#include <ctype.h>
 
using namespace std;
 
int main()
{
	
	//declare global variables
	int index = 0;
	string word;
 
	//input from user
	cout << "Input a word to return in ICAO form: "; 
	cin >> word;
	cout << endl;
 
	//the array, initialized with ICAO alphabet
	string ICAO[26] = {"Alpha", "Bravo", "Charlie", "Delta" , "Echo", "Foxtrot" , "Golf" , 
                      "Hotel" , "India" , "Juliet", "Kilo" , "Lima", "Mike" , "November" , 
                      "Oscar" , "Papa" , "Quebec", "Romeo" , "Sierra" , "Tango" , "Uniform" ,
                      "Victor" , "Whiskey" , "X-ray" , "Yankee" , "Zulu"};
 
	//loop while index number is less than the length of the input word
	while (index < (int)word.length())
	{
		//local variables
		string output;
		char letter;
		
		letter = word[index];			//get individual letter of input word
                if (!isalpha(letter)) { ++index; continue; }// <------- skip non-alphabetic input
		letter = toupper(letter);		//convert to uppercase letter 						
		output = ICAO[letter - 'A'];	//display proper string from array
		cout << output << " ";			//output will be on one line
		index++;						//increment the index
	}
 
cout << endl;
 
 
system("pause");
return 0;
}

Open in new window

Author

Commented:
Thanks, works great!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial