Im just learning C++ this year, and its going alright, until i came to arrays that have indexes with semantic content. I am asked to write program that will convert a user inputted string into the civil aviation alphabet, for example if teh user inputted "and" the output would be "Alpha November Delta" with a being alpha, n being november and so on. I want to type out the exact question cuz that would take a long time, and i dont think anyone wants to read it. But so far in my programming i have followed it exactly. My problem comes in the for loop at the end where i must call the appropriate string from my array.

#include <iostream> 
#include <string> 
#include <cctype> 
#include <iomanip> 
 
using namespace std; 
 
enum letters {A, B, C, D, E, F, G, H ,I, J, K, L, M, N, O, P, R, S, T, U, V, W, X, Y, Z}; 
 
void string2char(string, char[], int&); 
void errorCheck(char[], string&, int&, letters); 
void decipher(int, char[], const string[], letters&); 
 
int main() 
{ 
string ICAO[25] = {"Alpha", "Bravo", "Charlie", "Delta", "Foxtrot", "Echo", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey", "X-Ray", "Yankee", "Zulu"}; 
letters match; //will be used to match A with 
                   //Alpha, and so on 
string input; 
int len = 0; 
char inputArray[len]; 
 
cout << "This program will take an inputted string and\n"; 
cout << "convert it using the ICAO alphabet.\n"; 
cout << "Please enter string.\n"; 
cin >> input; 
 
//call 2 functions, 1st change string to array, 2nd error check 
string2char(input, inputArray, len); 
errorCheck(inputArray, input, len, match); 
 
decipher(len,inputArray,ICAO,match); 
 
return 0; 
} 
 
//i left out the errorchecking and string2char functions 
//but ive tested them and they work properly 
 
void decipher(int len, char inputArray[], const string ICAO[], letters& match) 
{ 
int i; 
for (i = 0; i < len; i++) 
{ 
//no idea, everything ive tried crashes program, im 
//assuming its an out of bounds error 
} 
return; 
}

any help you can give will be greatly appreciated, thanks.

ok, my latest attempt at the last for loop looks like this
i moved the enum from the top to inside the last function

void decipher(int len, char inputArray[], const string ICAO[])
{
enum letters {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, R, S, T, U, V, W, X, Y, Z};

letters match;
int i;

for (i = 0; i < len; i++)
{
do
{
if (inputArray[i] == match)
cout << ICAO[match];
else
match = letters(match + 1);
}while (inputArray[i] != match);
}
return;
}

btw still not working

int len = 0;

Anything that uses

len

wont work while it is 0.

char inputArray[len];
	string2char(input, inputArray, len);
	errorCheck(inputArray, input, len, match);

	decipher(len,inputArray,ICAO,match);
string ICAO[25] = {"Alpha", "Bravo", "Charlie", "Delta", "Foxtrot", "Echo", "Golf", "Hotel", "India", "Juliet", "Kilo", "Lima", "Mike", "November", "Oscar", "Papa", "Romeo", "Sierra", "Tango", "Uniform", "Victor", "Whiskey","X-Ray", "Yankee", "Zulu"};

You have to transpose the entries in red.

thanks for the reply wolf, i dont quite understand why it wont work with len=0, i only have len =0 for a short time, once the string is passed to a character array len=#of elements in array. Good eye noticing that E and F were backwards. Did you look at my latest attempt at the for loop.

My problem seems to be in going from the enum letter value, of say F, to the char 'F' or vice versa.

Made some modifications to your program.

enum letters {A = 'A', B, C, D, E, F, G, H ,I, J, K, L, M, N, O, P, R ,Q, S, T, U, V, W, X, Y, Z };

without this the value of A will be zero not ascii value of 'A'

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"};
void decipher(int len, char inputArray[], const string ICAO[])
{
letters match;
int i;

	for (i = 0; i < len; i++)
	{
		match = A; // Remember to Initialize your variables
		do
		{
			match = letters(match + 1);
		}while (inputArray[i] != match);

		cout << ICAO[match - A];
	}
	return;
}

And another thing. This will only work if you input capital letters. Modify it yourself for simple letters. You can do the decipering without the use of this while loop. Think again.

i dont quite understand why it wont work with len=0, i only have len =0 for a short time

but during that short time you are declaring a char array of zero elements.

char inputArray[len];

I dont think a compiler will allow that.

oops double postings. Sowwieee. :o

Tryst,

Maybe the string2char() call should include the reference operator & for len:

string2char(input, inputArray, &len);

So what was the finally Program on this what should it look like??? curious to know..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.