使用两个字符串数组的C ++ english to morse代码

时间:2016-04-18 17:39:03

标签: c++ arrays string text

我试图用英语接受一个字符串,然后将其转换为莫尔斯码,包括空格。认为这应该工作,但当我输入一个字符串进行转换,没有输出,我点击输入,它保持在输入字符串屏幕,任何帮助或建议非常感谢。

int counter, option;
int length = 0;
//const int arraysize = 39;
string morse[39] = (".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
"..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", 
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", 
"...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", 
".-.-.-", "--..--", "..--..");

string alphabet[39] = ( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", 
"Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Stop", ",", "?");

int main()
{
    string english = "", coded, encoded, decoded;
    char letter, blank = " ";
    cout.precision(2);
    cout.setf(ios::fixed | ios::showpoint);
    cout << "1 - Encode text to morse\n2 - Decode from morse to english\n3 - 
    Display Morse code\n4 - Quit\nSelect an option, 1 - 4: ";
    cin >> option;
    system("cls");

switch(option)
{
    case 1:
        morsedisplay();
        cout << "\nEnter the text that you want translated to Morse 
        Code: ";
        cin.ignore(100, 'n');
        getline(cin, english);
        length = english.length();
        for (int n = 0; n <= length - 1; n++)
        {
            letter = english.at(n);
            if (letter != ' ')
            {
                for(int counter = 0; counter < 39; counter++)
                {
                    string temporary;
                    temporary = toupper(letter);
                    if (temporary == alphabet[counter])
                    {
                        cout << morse[counter] << " ";
                        break;
                    }
                }
            }
        }

        break;
    case 2:

0 个答案:

没有答案