-
array program trouble
I have a program I am writing that I am having a lot of trouble with. I need to write a program that takes in a string and then spells that string using the ICAO alphabet. The ICAO alphabet needs to be in an array. How would I set the array up? Thank you. Here is the alphabet:
A Alpha
B Bravo
C Charlie
D Delta
E Echo
F Foxtrot
G Golf
H Hotel
I India
J Juliet
K Kilo
L Lima
M Mike
N November
O Oscar
P Papa
Q Quebec
R Romeo
S Sierra
T Tango
U Uniform
V victor
W Whiskey
X X-ray
Y Yankee
Z Zulu
-
Like you set up normal arrays ???
You may also want to use std::map if you like.
Code:
std::string array[] = { "alpha", "bravo", ... "zulu" };
char ch = 'A';
std::cout<<array[ch - 65];
std::cout<<array[ch - 'A'];
-
Why not:
Code:
const std::string ICAO[] = { "Alpha", "Bravo", "Charlie", /*...*/ };
Then all you have to do is figure out how to get the correct subscript.
Edit: A few minutes too slow...
-
Code:
#include <string>
std::string ICAO[26] = {Alpha, Bravo, ...};
I think something like above may be one way to approach the problem. I'm pretty sure the index of the array is defined here, but the index may be required to be absent in this type of declaration.
The above will work because if you note in an ASCII table A-Z is equivalent to the integers 65-90 so you can just take a letter say "B" convert it to an integer than subtract 65 to get the valid index into the array, which would be 66-65=1.
I'm sure there are countless ways to approach this problem.
Edit: A few minutes too slow times 2....