I'm working on a fun random ICAO translator program and I'm almost done with it but I'm having one little problem. How do I go about splitting a string by each character? For example the output I want is; The word mike translated in the ICAO alphabet is:
m: Mike
i: Indiana
k: Kilo
e: Echo
So far I just get; The word mike translated in the ICAO alphabet is:
Mike
Indiana
Kilo
Echo
Apparently my post is mostly code and I must add more detail so I'm adding this sentence to hopefully satisfy the requirements. Also the translation should be right on top of each other and not one extra space down. I'm having problems with that and idk how to fix that.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = " ", phonetic;
int count = 0;
cout << "Enter a word: ";
cin >> word;
while(count < word.length())
{
switch(word.at(count))
{
case 'A': case 'a': phonetic += " Alpha\n";
break;
case 'B': case 'b': phonetic += " Bravo\n";
break;
case 'C': case 'c': phonetic += " Charlie\n";
break;
case 'D': case 'd': phonetic += " Delta\n";
break;
case 'E': case 'e': phonetic += " Echo\n";
break;
case 'F': case 'f': phonetic += " Foxtrot\n";
break;
case 'G': case 'g': phonetic += " Golf\n";
break;
case 'H': case 'h': phonetic += " Hotel\n";
break;
case 'I': case 'i': phonetic += " Indiana\n";
break;
case 'J': case 'j': phonetic += " Juliet\n";
break;
case 'K': case 'k': phonetic += " Kilo\n";
break;
case 'L': case 'l': phonetic += " Lima\n";
break;
case 'M': case 'm': phonetic += " Mike\n";
break;
case 'N': case 'n': phonetic += " November\n";
break;
case 'O': case 'o': phonetic += " Oscar\n";
break;
case 'P': case 'p': phonetic += " Papa\n";
break;
case 'Q': case 'q': phonetic += " Quebec\n";
break;
case 'R': case 'r': phonetic += " Romeo\n";
break;
case 'S': case 's': phonetic += " Sierra\n";
break;
case 'T': case 't': phonetic += " Tango\n";
break;
case 'U': case 'u': phonetic += " Uniform\n";
break;
case 'V': case 'v': phonetic += " Victor\n";
break;
case 'W': case 'w': phonetic += " Whiskey\n";
break;
case 'X': case 'x': phonetic += " X-Ray\n";
break;
case 'Y': case 'y': phonetic += " Yankee\n";
break;
case 'Z': case 'z': phonetic += " Zulu\n";
break;
default: cout << "You did not enter a name" << endl;
}
count++;
}
cout << "The word "<< word <<" in the ICAO alphabet is:\n"
<< phonetic << endl;
return 0;
}
phoneticstring. So, just add another string, before each one of these, containing each character in question?phonetic += word.at(count)before the switch?phonetic +=word.at(count)before the switch is what I was looking for/missing. Thank you superStormer. I have now learned how to iterate a string a through it's characters.