【发布时间】:2014-12-06 00:11:43
【问题描述】:
注意:已解决,问题不是 getline() 而是 find 函数 数组填充不当!
在发布我自己的问题之前,我已经查找了几个问题,但我找不到我的问题的答案。这是我发布的第一个问题,但在发布我自己的问题之前,我确实做了一些研究并尝试了其他问题的其他解决方案。所以我不完全确定这不是重复的。我很抱歉!感谢您提前理解!
我正在尝试使用 getline() (c++) 来获取用户输入。它在我的 main 中运行良好,但在我的用户定义函数中却没有。我认为这可能与缓冲区有关,所以我按照以下建议使用了 cin.ignore():
C++ getline method not working
我检查了:
How does getline work with cin?
以确保我正确理解 getline()。但是我的程序仍然无法正常运行。
我的程序从用户输入(控制台输入)获取英文文本作为字符串,并将其转换为摩尔斯电码并将结果作为字符串输出(控制台输出)。
基本上我的问题是这样的:
getline 在我的主函数中适用于字符串和带空格的字符串,例如:“This”和“This Code”。
但是,在我的用户定义函数中,它仅适用于没有空格的字符串,例如:“This”。
感谢您的帮助!代码如下:sn-ps!
#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;
}
但是,如果我编辑我的代码并从我的 main 获取输入并将其作为参数传递,它就可以工作。
#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”。在该函数中,由于某种原因,当我输入“此代码”之类的字符串或中间有空格的任何字符串时,它不起作用。
注意:程序尚未完成,因为我计划添加其他方法来执行相反的操作(如代码中所示 额外的用户选项,但这些还没有实现或定义,代码仍然运行和编译我面临的问题。
【问题讨论】:
-
在您的第一个示例中,您调用
morseToText,而不是textToMorse。是不是笔误? -
我怀疑你不知何故超出了 morseAlphabet 数组的末尾。您确定它在“其余代码”之前崩溃吗?确定崩溃位置的一种方法是将 cout 语句放在整个代码中以进行调试,但希望您的调试器能比这更有帮助。如果您使用的是 Linux,valgrind 可以帮助您确定程序崩溃的位置。
-
我确实试过这个,我在 Windows 8.1 上使用 Code::Blocks。但数组似乎工作正常!
-
我修复了您的代码,使其能够编译,但由于
alphabet中没有空格字符而导致崩溃,因此find返回std::string::npos并尝试将越界索引到morseAlphabet。 -
谢谢!然而,我认为这不是问题,因为它在我的 main 中使用完全相同的代码工作(数组在 main 中未更改但仍然适用于空格),但这确实在我的函数中修复了它!如果没有数组中的空间,它会在 main() 中工作而不是在我的函数中工作吗?