getline输入较长的问题 我写了一个C++程序,它要求用户输入一个单词或句子,通过单词/句子,用“AOA”或“AOA”替换所有“A”或“A”的实例,然后输出结果。然而,如果我试图输入一个较长的句子,我会遇到问题。例如,如果我键入“为什么程序不运行”,程序将输出奇怪的字母,而不是预期的结果。 这是我的代码: #include <iostream> #include <string> using namespace std; int main(int argc, const char * argv[]) { string mening, temp; //The mening string is the word/sentence the user will input. int play = 1, add; while (play == 1) { cout<<"Type in the sentence: "; getline(cin, mening); //The input is saved in the string variable mening. unsigned long y = mening.size(); //Grabs the amount of characters in input; this number is saved in the unsigned long variable y. add = 0; //Makes sure the int variable add is reset to 0 if the loop restarts. for (int k = 0, n = 1;n<=y;k++, n++) { if (mening[k] == 'a' || mening[k] == 'A') { k++; for (int i = k, m = 1;m<=y - n;i++, m++) { temp[i] = mening[i]; } //The characters after the one that has been checked are stored in temp array indexes, if the character that has been checked is an a or A. for (int i = k, m = 1, j = k + 2;m<=y - n;i++, m++, j++) { mening[j] = temp[i]; } //The characters after the one that has been checked move two steps to the right, to allow the two extra letters. mening[k] = 'o'; mening[k + 1] = mening[k - 1]; k++; add = add + 2; //The int variable add is increased by 2 during each aoa/AoA to avoid strange characters being outputted at the very end. } else { } } for (int k = 0;k<=y + add - 1;k++) { cout<<mening[k]; } cout<<endl<<"Do you want to do it again? (yes/no): "; getline(cin, mening); cin.clear(); cout << flush; cout.flush(); cout.clear(); if (mening == "Yes" || mening == "yes" || mening == "YES") { } else { play = 2; } } cout<<endl<<"The program will now close."; return 0; } #包括 #包括 使用名称空间std; int main(int argc,const char*argv[] { 字符串mening,temp;//mening字符串是用户将输入的单词/句子。 int play=1,相加; while(play==1){ cout
我建议您应该使用标准函数getline输入较长的问题 我写了一个C++程序,它要求用户输入一个单词或句子,通过单词/句子,用“AOA”或“AOA”替换所有“A”或“A”的实例,然后输出结果。然而,如果我试图输入一个较长的句子,我会遇到问题。例如,如果我键入“为什么程序不运行”,程序将输出奇怪的字母,而不是预期的结果。 这是我的代码: #include <iostream> #include <string> using namespace std; int main(int argc, const char * argv[]) { string mening, temp; //The mening string is the word/sentence the user will input. int play = 1, add; while (play == 1) { cout<<"Type in the sentence: "; getline(cin, mening); //The input is saved in the string variable mening. unsigned long y = mening.size(); //Grabs the amount of characters in input; this number is saved in the unsigned long variable y. add = 0; //Makes sure the int variable add is reset to 0 if the loop restarts. for (int k = 0, n = 1;n<=y;k++, n++) { if (mening[k] == 'a' || mening[k] == 'A') { k++; for (int i = k, m = 1;m<=y - n;i++, m++) { temp[i] = mening[i]; } //The characters after the one that has been checked are stored in temp array indexes, if the character that has been checked is an a or A. for (int i = k, m = 1, j = k + 2;m<=y - n;i++, m++, j++) { mening[j] = temp[i]; } //The characters after the one that has been checked move two steps to the right, to allow the two extra letters. mening[k] = 'o'; mening[k + 1] = mening[k - 1]; k++; add = add + 2; //The int variable add is increased by 2 during each aoa/AoA to avoid strange characters being outputted at the very end. } else { } } for (int k = 0;k<=y + add - 1;k++) { cout<<mening[k]; } cout<<endl<<"Do you want to do it again? (yes/no): "; getline(cin, mening); cin.clear(); cout << flush; cout.flush(); cout.clear(); if (mening == "Yes" || mening == "yes" || mening == "YES") { } else { play = 2; } } cout<<endl<<"The program will now close."; return 0; } #包括 #包括 使用名称空间std; int main(int argc,const char*argv[] { 字符串mening,temp;//mening字符串是用户将输入的单词/句子。 int play=1,相加; while(play==1){ cout,c++,input,getline,cin,C++,Input,Getline,Cin,我建议您应该使用标准函数std::string::insert来插入字符'oA'或'oA'。这将使您的代码更易于处理和调试 或者,您也可以像这样做: #include <iostream> #include <string> using namespace std; int main(int argc, const char * argv[]) { string mening, temp; //The mening string is the word/sentenc
std::string::insert
来插入字符'oA'
或'oA'
。这将使您的代码更易于处理和调试
或者,您也可以像这样做:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
string mening, temp; //The mening string is the word/sentence the user will input.
int play = 1, add;
while (play == 1) {
cout<<"Type in the sentence: ";
getline(cin, mening); //The input is saved in the string variable mening.
unsigned long y = mening.size(); //Grabs the amount of characters in input; this number is saved in the unsigned long variable y.
add = 0; //Makes sure the int variable add is reset to 0 if the loop restarts.
for (int n = 0; n<y; n++ ) {
cout << mening[n];
if (mening[n] == 'a' || mening[n] == 'A')
cout << "o" << mening[n];
}
cout<<endl<<"Do you want to do it again? (yes/no): ";
getline(cin, mening);
cin.clear();
cout << flush;
cout.flush();
cout.clear();
if (mening == "Yes" || mening == "yes" || mening == "YES") {
}
else {
play = 2;
}
}
cout<<endl<<"The program will now close.";
return 0;
}
#包括
#包括
使用名称空间std;
int main(int argc,const char*argv[]
{
字符串mening,temp;//mening字符串是用户将输入的单词/句子。
int play=1,相加;
while(play==1){
cout一个直接的问题是,您正在从[1,n]
建立索引,
而C++(<代码> STD::String , STD::vector < /C>),也是C风格
数组)使用[0,n)
。这意味着您将在
字符串的结尾。您正在将字符存储到temp
使用temp[x]
,尽管temp
的大小始终为0。两者
这些都是未定义的行为,可能会产生任何影响
(包括使程序崩溃)
在以下情况下,应使用标准库的调试模式:
开发代码。在Visual Studio中,我认为这是
默认设置;对于g++,您需要添加-D\u GLIBCXX\u概念\u检查
-D_GLIBCXX_DEBUG-D_GLIBCXX_DEBUG_PEDANTIC
到您的命令
线路
最简单的处理方法是复制到新字符串中,
在进行更改时执行以下操作:
std::string results;
for ( auto current = mening.cbegin(); current != mening.cend(); ++ current ) {
switch ( *current ) {
case 'a':
results += "aoa";
break;
case 'A':
results += "AoA";
break;
default:
results += *current;
break;
}
}
如果你真的想在适当的位置进行替换,这会变得很棘手。
当插入的文本比开始时多时,迭代器将被禁用
无效。所以您需要类似以下内容:
static std::string const Ao( "Ao" );
static std::string const ao( "ao" );
for ( auto current = mening.begin(); current != mening.end(); ++ current ) {
switch ( *current ) {
case 'a':
current = mening.insert( current, ao.begin(), ao.end() ) + 2;
break;
case 'A':
current = mening.insert( current, Ao.begin(), Ao.end() ) + 2;
break;
}
}
就我个人而言,我倾向于复制到一个新字符串中。实际上,上一个cin>>还有一个尾随字符“\n”,因此getline接受这个“\n”字符并终止;忽略getline之前的缓冲区内容
#include <iostream>
#include <string>
std::string ReplaceA(std::string s) {
std::string temp = "";
for (unsigned int k = 0; k < s.size(); k++) {
if (s[k] == 'a' || s[k] == 'A') {
temp = temp + s[k];
temp = temp + "o";
temp = temp + s[k];
}
else {
temp = temp + s[k];
}
}
return temp;
}
int main(int argc, const char * argv[])
{
std::string mening; //The mening string is the word/sentence the user will input.
int play = 1;
while (play == 1) {
std::cout<<"Type in the sentence: ";
getline(std::cin, mening); //The input is saved in the string variable mening.
std::cout << ReplaceA(mening) << std::endl;
std::cout << "Do you want to do it again? (yes/no): ";
std::cin >> mening;
if( std::cin.fail() || ( mening != "yes" && mening != "no" ) ) {
std::cout << "Bad Input\nDo you want to do it again? (yes/no): ";
std::cin.clear();
std::cin.ignore('256','\n');
std::cin >> mening;
}else{
if(mening == "no") break;
}
std::cin.clear();
std::cin.ignore('256','\n');
}
std::cout << "The program will now close.";
return 0;
}
cin.ignore();
getling(cin,string_name)我会坐下来重新思考你的算法。这个想法很复杂。你只需要迭代字母一次,然后构建一个新字符串。我打赌你是在写字符串的结尾。使用内置的字符串成员函数来处理它的内容。(如插入或擦除).PS.代码格式非常重要。请让它更容易阅读。洛基:关于代码格式,我想知道你具体指的是什么。顺便说一下,这是我在Stack Overflow上的第一篇文章。是的,我认为不需要存储新句子。只需在处理时输出它。将转换分解成单独的函数ion是个好主意。但只是一个建议:在返回值上使用+=
,而不是更复杂的temp=temp+…
。是的,我们正在跟踪一个bug。temp+=s[k]+“o”+s[k];
给了我意外的输出,所以我只是写了很长时间来修复它。可能是s[k]+“o”+s[k]
是个问题说实话,我会用一个开关,把'a'
和'a'
分开处理,这样我就可以写temp+=“aoa”
或temp+=“aoa”
。就我个人而言,这似乎有点简单。无论如何,对我来说。(一旦您停止尝试进行适当的更改,并将其分解成单独的函数,就像您所做的那样,细节就真的没有那么重要了。)@LokiAstari我建议您查看代码。我在insert
之后使用insert
返回的迭代器。(另一方面,我没有得到正确的返回值。我会纠正的。)James,你的评论太好了。我不知道在为字符串指定数组值之前必须为字符串指定一个大小,也不知道必须从0开始指定,因为在使用字符串时不必声明数组。如果这是一个非常愚蠢的问题,我很抱歉。@månsNilsson一点也不愚蠢。似乎很多人都这么做了开始时出现错误。但这就是为什么我们在向量上使用+=
(或向后推
)。这些函数会自动增加字符串。我已经接受了你的第一个建议,James。当你可以将每个字母复制到一个新字符串中时,为什么还要经历重新排列数组数等痛苦呢?我不知道你可以添加到一个字符串中。现在我只有60行代码,它工作得非常好!很明显,这是一个非常好的例子目前,我还是一名编程新手。但希望我在下一个项目中能考虑到这些因素。:)@MånsNilsson我自己通常在第一个解决方案中使用变体。当我尝试第二个解决方案时,我有点惊讶于它导致代码行数减少,但我仍然发现第一个更干净,更容易理解和修改。(第二个困惑的Loki Astari,他是这里的常客,而且对C++很熟悉。迭代器到一个正在发生突变的容器中的规则有些复杂。)
std::string ReplaceA(std::string s) {
std::string temp = "";
for(std::string::iterator k = s.begin(); k != s.end(); k++) {
switch(*k) {
case 'a': temp += "aoa"; break;
case 'A': temp += "AoA"; break;
default: temp += *k; break;
}
}
return temp;
}