gpt4 book ai didi

c++ - 较长的 getline cin 输入问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:26:19 26 4
gpt4 key购买 nike

我正在编写一个 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;
}

可能导致问题的原因是什么?

最佳答案

一个紧迫的问题是您从 [1,n] 建立索引,而 C++(std::stringstd::vector,还有 C 风格数组)使用 [0,n)。这意味着您将可以访问字符串的结尾。你正在将字符存储到 temp使用 temp[x],尽管 temp 的大小始终为 0。两者这些是未定义的行为,可能会产生任何影响(包括使程序崩溃)。

你应该使用标准库的 Debug模式开发代码。在 Visual Studios 中,我认为这是默认;使用 g++,您需要添加 -D_GLIBCXX_CONCEPT_CHECKS
-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;
}
}

就我个人而言,我更喜欢复制到一个新字符串中。

关于c++ - 较长的 getline cin 输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18877635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com