ICAO example

Write your question here.
I am needing help using string input and using the switch statement in order to output the Phonetic version of the string entered. All examples I can find have a form of overloading or array, however, I cannot use that because we have not covered those in my class yet. How can I fix my program in order to output correctly. Any help would be appreciated, thanks.
[code]
Put the code you need help with here.
#include <iostream>
#include <string>
using namespace std;

int main()
{
int count;
string Phonetic = " ";
string word;
char letter;

cout << "Enter a single word to be converted to the Phonetic Alphabet: " << endl;
getline(cin, word);

for(count = 0; count; count++)
{
letter = count++;
switch (letter)
{
case 'a':
case 'A': Phonetic += "Alpha ";
break;
case'b':
case'B': Phonetic += "Bravo ";
break;
case'c':
case'C': Phonetic += "Charlie ";
break;
case'd':
case'D': Phonetic += "Delta ";
break;
case'e':
case'E': Phonetic += "Echo ";
break;
case'f':
case'F': Phonetic += "Foxtrot ";
break;
case'g':
case'G': Phonetic += "Golf ";
break;
case'h':
case'H': Phonetic += "Hotel ";
break;
case'i':
case'I': Phonetic += "India ";
break;
case'j':
case'J': Phonetic += "Juliet ";
break;
case'k':
case'K': Phonetic += "Kilo ";
break;
case'l':
case'L': Phonetic += "Lima ";
break;
case'm':
case'M': Phonetic += "Mike ";
break;
case'n':
case'N': Phonetic += "November ";
break;
case'o':
case'O': Phonetic += "Oscar ";
break;
case'p':
case'P': Phonetic += "Papa ";
break;
case'q':
case'Q': Phonetic += "Quebec ";
break;
case'r':
case'R': Phonetic += "Romeo ";
break;
case's':
case'S': Phonetic += "Sierra ";
break;
case't':
case'T': Phonetic += "Tango ";
break;
case'u':
case'U': Phonetic += "Uniform ";
break;
case'v':
case'V': Phonetic += "Victor ";
break;
case'w':
case'W': Phonetic += "Whiskey ";
break;
case'x':
case'X': Phonetic += "X-ray ";
break;
case'y':
case'Y': Phonetic += "Yankee ";
break;
case'z':
case'Z': Phonetic += "Zulu ";
break;
}
}
cout << "Phonetic version of your word is: " << Phonetic << endl;
return 0;
}
1
2
3
4
5
getline(cin, word);

for(int K = 0; K<word.size(); ++K)
{
   letter = word[K];
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string phonetic;
   string word;
   cout << "Enter a single word to be converted to the phonetic alphabet: ";
   getline( cin, word );
   for ( char c : word )
   {
      switch ( c )
      {
         case 'a': case 'A': phonetic += "Alpha"   ;   break;
         case 'b': case 'B': phonetic += "Bravo"   ;   break;
         case 'c': case 'C': phonetic += "Charlie" ;   break;
         case 'd': case 'D': phonetic += "Delta"   ;   break;
         case 'e': case 'E': phonetic += "Echo"    ;   break;
         case 'f': case 'F': phonetic += "Foxtrot" ;   break;
         case 'g': case 'G': phonetic += "Golf"    ;   break;
         case 'h': case 'H': phonetic += "Hotel"   ;   break;
         case 'i': case 'I': phonetic += "India"   ;   break;
         case 'j': case 'J': phonetic += "Juliet"  ;   break;
         case 'k': case 'K': phonetic += "Kilo"    ;   break;
         case 'l': case 'L': phonetic += "Lima"    ;   break;
         case 'm': case 'M': phonetic += "Mike"    ;   break;
         case 'n': case 'N': phonetic += "November";   break;
         case 'o': case 'O': phonetic += "Oscar"   ;   break;
         case 'p': case 'P': phonetic += "Papa"    ;   break;
         case 'q': case 'Q': phonetic += "Quebec"  ;   break;
         case 'r': case 'R': phonetic += "Romeo"   ;   break;
         case 's': case 'S': phonetic += "Sierra"  ;   break;
         case 't': case 'T': phonetic += "Tango"   ;   break;
         case 'u': case 'U': phonetic += "Uniform" ;   break;
         case 'v': case 'V': phonetic += "Victor"  ;   break;
         case 'w': case 'W': phonetic += "Whiskey" ;   break;
         case 'x': case 'X': phonetic += "X-ray"   ;   break;
         case 'y': case 'Y': phonetic += "Yankee"  ;   break;
         case 'z': case 'Z': phonetic += "Zulu"    ;   break;
      }
      phonetic += ' ';
   }
   cout << "Phonetic version of your word is: " << phonetic << '\n';
}

Enter a single word to be converted to the phonetic alphabet: cplusplus
Phonetic version of your word is: Charlie Papa Lima Uniform Sierra Papa Lima Uniform Sierra



Here's a simpler one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <map>
using namespace std;

string ICAO[] = { "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" };

int main()
{
   map<char,string> M;
   for ( int i = 0; i < 25; i++ ) M['a'+i] = M['A'+i] = ICAO[i];
   
   string word, phonetic;
   cout << "Enter a single word to be converted to the phonetic alphabet: ";
   getline( cin, word );
   for ( char c : word ) if ( M.count(c) ) phonetic += M[c] + ' ';
   cout << "Phonetic version of your word is: " << phonetic << '\n';
}
Last edited on
Your first example was exactly what I needed help with, I just couldn't figure out how to get each character to pull from the string, but that is very clean as well. Thank you so much for your help!
Topic archived. No new replies allowed.