如何使用switch语句将单词转换为ICAO字母?

时间:2019-07-12 01:04:53

标签: c++ while-loop switch-statement

我正在尝试编写一个程序,将用户输入的单词转换为国际民用航空组织的字母,该字母必须使用switch语句而不是一系列的if语句。我能够将其用于单字符输入,但是当我尝试修改程序以使其通过添加while循环来转换一系列字母时,转换变得不准确。

如上所述,我试图添加一个while循环以使程序读取输入单词中的每个字符,从而导致问题。

#include <iostream>
using namespace std;

int main()
{
    //Declare needed variables
    string word = " ";
    int count = 0;

    //Prompt user input
    cout << "Enter a word: ";
    cin >> word;

    while(count < word.length())
    {

    switch (word[count])
    {
        case 'A': case 'a': word = "Alpha";
            break;
        case 'B': case 'b': word = "Bravo";
            break;
        case 'C': case 'c': word = "Charlie";
            break;
        case 'D': case 'd': word = "Delta";
            break;
        case 'E': case 'e': word = "Echo";
            break;
        case 'F': case 'f': word = "Foxtrot";
            break;
        case 'G': case 'g': word = "Golf";
            break;
        case 'H': case 'h': word = "Hotel";
            break;
        case 'I': case 'i': word = "Indiana";
            break;
        case 'J': case 'j': word = "Juliet";
            break;
        case 'K': case 'k': word = "Kilo";
            break;
        case 'L': case 'l': word = "Lima";
            break;
        case 'M': case 'm': word = "Mike";
            break;
        case 'N': case 'n': word = "November";
            break;
        case 'O': case 'o': word = "Oscar";
            break;
        case 'P': case 'p': word = "Papa";
            break;
        case 'Q': case 'q': word = "Quebec";
            break;
        case 'R': case 'r': word = "Romeo";
            break;
        case 'S': case 's': word = "Sierra";
            break;
        case 'T': case 't': word = "Tango";
            break;
        case 'U': case 'u': word = "Uniform";
            break;
        case 'V': case 'v': word = "Victor";
            break;
        case 'W': case 'w': word = "Whiskey";
            break;
        case 'X': case 'x': word = "X-Ray";
            break;
        case 'Y': case 'y': word = "Yankee";
            break;
        case 'Z': case 'z': word = "Zulu";
            break;
        default: cout << "You did not enter a letter." << endl;
    }

    count++;
}
cout << "Phoenetic version is: " << word << endl;

return 0;
}

此代码在当前状态下编译,但没有给我期望的结果。

1 个答案:

答案 0 :(得分:0)

您遇到的最大问题是,每次分配ICAO语音值时,例如,更改word的值。 word = "Alpha";您需要使用一个单独的值来构建结果字符串,例如

    //Declare needed variables
    string word = " ", phonetic {};
    unsigned count = 0;
    ...
    while(count < word.length())
    {
        switch (word[count])
        {
            case 'A': case 'a': phonetic += " Alpha";
                break;
            case 'B': case 'b': phonetic += " Bravo";
                break;
            ...

注意:将unsigned的{​​{1}}更改为防止有符号和无符号值之间的比较,并使用count附加每个语音单词到+=-以及每个空格之前添加的空格)

现在将ICAO语音结果存储在phonetic中,您可以输出结果:

phonetic

将其完全放在一起,您可以执行以下操作:

    cout << "Phoenetic version is:" << phonetic << endl;

注意:,请考虑使用#include <iostream> using namespace std; int main (void) { //Declare needed variables string word = " ", phonetic {}; unsigned count = 0; //Prompt user input cout << "Enter a word: "; cin >> word; while(count < word.length()) { switch (word[count]) { 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 += " Indiana"; 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; default: cout << "You did not enter a letter." << endl; } count++; } cout << "Phoenetic version is:" << phonetic << endl; return 0; } 而不是word.at(count)来在索引单个字符时启用边界检查-在这里使用word[count]是安全的,但是通常word.length()提供了更高级别的验证)

使用/输出示例

.at()

仔细研究一下,如果您有任何疑问,请告诉我。