1

处理我的第一个编程任务,一个文本到莫尔斯(和返回)转换器,但无论出于何种原因,当我引入一段文字之间有空格的文本时,我的程序进入无限循环并崩溃。有任何想法吗?对不起,如果这个描述很糟糕,我仍然在思考编程术语。

这是程序无法正常运行的部分:

    {

        string user_input;
        cout << "----------------------------------------" <<endl
        << "Text to Morse Mode" << endl
        << "Enter text for conversion : "<<endl;
        cin >> user_input;
        cout << endl << endl << user_input << " converts to : ";
        unsigned int str_lenght;
        str_lenght=user_input.size();
        cout << endl;

        for (i=0;i<str_lenght;i++)
        {

            find_string=0;

            while (find_string < stop_string)
            {


                if (user_input[i]==text[find_string][0])
                {
                    count=1;
                    cout << morse[find_string] << " ";
                    break;


                }

                find_string = find_string+1;
            }

        }

        cout << endl << endl << endl;

        if (count==0)
            cout << endl << " an error was encountered " << "\a" << endl ;
    }
4

2 回答 2

0

First you haven't defined stop_string variable anywhere. First define it or use another variable. If it is string length intent to use here, use the str_length you have created. Secondly if you want to input spaces in between your words, use getline instead of cin. cin delimits space character.

于 2012-08-26T17:41:27.197 回答
0

stop_string从我看到的任何地方都没有定义。为了通过递增来打破循环,您需要定义stop_string. 也find_string = find_string+1;可以缩短为find_string++

于 2012-08-26T17:36:56.933 回答