c++ - 注意 :(getline was not the issue) C++ getline() stops working in user defined function but works in main function

标签 c++ find

注意:已解决,问题不是 getline() 而是 find 函数 数组填充不正确!

在发布自己的问题之前,我已经查找了几个问题,但找不到问题的答案。这是我发布的第一个问题,但在发布我自己的问题之前,我确实做了一些研究并尝试了其他问题的其他解决方案。所以我不完全确定这不是重复的。我很抱歉!感谢您提前理解!

我正在尝试使用 getline() (c++) 来获取用户输入。它在我的主函数中运行良好,但在我的用户定义函数中不起作用。我认为这可能与缓冲区有关,所以我按照以下建议使用了 cin.ignore():

C++ getline method not working

我查了一下:

How does getline work with cin?

以确保我正确理解 getline()。但是我的程序仍然无法正常工作。

我的程序将用户输入(控制台输入)中的英文文本作为字符串,并将其转换为莫尔斯电码,并将结果作为字符串输出(控制台输出)。

基本上我的问题是这样的:

getline 在我的主函数中适用于字符串和带空格的字符串,例如:“This”和“This Code”。

但是,在我的用户定义函数中,它仅适用于不带空格的字符串,例如:“This”。

感谢您的帮助!下面是代码片段!

#include <iostream>;
#include <stdio.h>;
#include <ctype.h>;

using namespace std;


string textToMorse(const string alphabet, const string morseAlphabet[]);

int main()
{
    const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?";
    const string morseAlphabet[39] = {".-","-...","-.-.","-..",".","..-.","--.","....","
    ..",".---","-.-",".-..","--","-.","---",".--.",
    "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---",
    "...--","....-",".....",
    "-....","--...","---..","----.",".-.-.-","--..--","..--.."};
    int userSelection;
    string resultString;


    cout << "TEXT TO MORSE CODE or MORSE CODE TO TEXT program" << endl << endl;
    cout << "Please select an option by typing the integer shown: " << endl << endl;
    cout << "Type(Selects option) 1 to decode Morse code to English text" << endl;
    cout << "Type(Selects option) 2 to encode English text to Morse code" << endl;
    cout << "Type(Select option) any other integer that is NOT 1 or 2 to QUIT" << endl << endl;
    cin >> userSelection;

   while(userSelection == 1 || userSelection == 2)
   {

        if(userSelection == 1)
        {

            resultString = textToMorse(alphabet, morseAlphabet); // function where I use 
                                                                 // getline() but does not work
            cout << endl << "This is the Morse code decoded to English text: " << endl << endl;
            cout << resultString << endl << endl << endl << endl;
        }
   }

    return 0;
}

// does not work
string textToMorse(const string alphabet, const string morseAlphabet[])
{
    string userInput;
    cout << endl << "Enter English text to encode to Morse code,
    with only a space between words: " << endl << endl;

    cin.ignore();
    getline(cin,userInput); //code works with strings without spaces, 
                            //but breaks with others. ex: "This" works as input
                            //but "This code" breaks and the console seems to freeze
                            // then crashes out

    cin.clear();

    // rest of code, but program breaks before this.

    string encodedEnglishText = "";

    for(int i = 0; i < userInput.length(); i++)
    {
        userInput[i] = toupper(userInput[i]);
    }

    for(int i = 0; i < userInput.length(); i++)
    {
       encodedEnglishText += morseAlphabet[alphabet.find(userInput[i])];
       encodedEnglishText += " "; // extra spacing added for output clarity

       if(userInput[i] == ' ')
       {
           encodedEnglishText += "  "; // extra spacing added for output clarity
       }
    }

    return encodedEnglishText;
}

但是,如果我编辑代码并从主程序获取输入并将其作为参数传递,它就可以工作。

#include <iostream>;
#include <stdio.h>;
#include <ctype.h>;

using namespace std;


string textToMorse(const string alphabet, const string morseAlphabet[], string userInput);
int main()
{
    const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?";
    const string morseAlphabet[39] = {".-","-...","-.-.","-..",".","..-.","--.","....","
    ..",".---","-.-",".-..","--","-.","---",".--.",
    "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---",
    "...--","....-",".....",
    "-....","--...","---..","----.",".-.-.-","--..--","..--.."};
    int userSelection;
    string resultString;


    cout << "TEXT TO MORSE CODE or MORSE CODE TO TEXT program" << endl << endl;
    cout << "Please select an option by typing the integer shown: " << endl << endl;
    cout << "Type(Selects option) 1 to decode Morse code to English text" << endl;
    cout << "Type(Selects option) 2 to encode English text to Morse code" << endl;
    cout << "Type(Select option) any other integer that is NOT 1 or 2 to QUIT" << endl << endl;
    cin >> userSelection;

   while(userSelection == 1 || userSelection == 2)
   {

        if(userSelection == 1)
        {
            string userInput;
            cout << endl << "Enter English text to encode to Morse code,
            with only a space between words: " << endl << endl;

            cin.ignore();
            getline(cin,userInput); //code works with both "This" and "This code"

            cin.clear();
            resultString = textToMorse(alphabet, morseAlphabet, userInput); //function modified 
                                                                           //to take one more
                                                                           //parameter
            cout << endl << "This is the Morse code decoded to English text: " << endl << endl;
            cout << resultString << endl << endl << endl << endl;
        }
   }

    return 0;
}

string textToMorse(const string alphabet, const string morseAlphabet[], string userInput)
{
    //code, but program works.

    string encodedEnglishText = "";

    for(int i = 0; i < userInput.length(); i++)
    {
        userInput[i] = toupper(userInput[i]);
    }

    for(int i = 0; i < userInput.length(); i++)
    {
       encodedEnglishText += morseAlphabet[alphabet.find(userInput[i])];
       encodedEnglishText += " "; // extra spacing added for output clarity

       if(userInput[i] == ' ')
       {
           encodedEnglishText += "  "; // extra spacing added for output clarity
       }
    }

    return encodedEnglishText;
}

我没有包含所有代码,仅包含我认为与问题相关的部分。

我所说的作品是指:

getline 成功获取输入。 getline 在 main 函数中使用时,成功将“this”、“this code”等字符串赋给变量 userInput。

当在我的用户定义函数中使用时,它只能成功分配不带空格的字符串,例如“this”。在该函数中,由于某种原因,当我输入“此代码”之类的字符串或任何中间有空格的字符串时,它不起作用。

注意:程序尚未完成,因为我计划添加其他方法来执行相反的操作(如代码中所示) 额外的用户选项,但这些尚未实现或定义,代码仍然可以针对我面临的问题运行和编译。

最佳答案

问题在于空格没有摩尔斯电码。

进行验证:

    int n = alphabet.find(userInput[i]);
    encodedEnglishText += (n == string::npos) ? "  ":  morseAlphabet[n];

然后就可以了。

关于c++ - 注意 :(getline was not the issue) C++ getline() stops working in user defined function but works in main function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326718/

相关文章:

c++ - 我将如何实现 FCFS 处理器调度模拟器?

javascript - 在 javascript 中查找上下文相关的子字符串

php - MongoDB PHP 限制不起作用

c++ - 主机不会加载 VST 插件效果

c++ - 3.4.1 非限定名称查找

javascript - jQuery:设置/重置具有特定类的表单字段(改进代码)

c++ - 在二叉搜索树中查找元素仅在 true 时有效

c++ - 如何从http请求正文字符串中获取文件名?

c++ - 亚信 : usage of self shared pointer in the examples

c++ - delete[] 指向数组的指针,数组大小的额外 4 字节存储在哪里?