gpt4 book ai didi

C++ 省略或添加额外字符

转载 作者:行者123 更新时间:2023-11-28 06:52:16 26 4
gpt4 key购买 nike

所以我有一个有趣的错误。我有这段代码(这是部分代码):

 if (isQuote(ch))
{
//Add it to the Buffer
strBuffer += ch;

do
{
//get Next Character from ifstream and add it to the strBuffer
getNextAndAdd(ch);


// check whether it is a \\'
if (isBackslash(ch) && (this -> peek() == '"'))
{
//if it is a backslash, activate check checkQuote
checkQuote = true;
}
//if it is another quote, dectivate check Quote otherwise, break the system
if (isQuote(ch))
{
if (checkQuote)
{
checkQuote = false;
}
else
{
break;
}
}
//IF EOF, do NOT continue repeating the last character
if (input -> eof()){break;}

}
while (isPrintable(ch)); // check that it falls within the SXL acceptance range
if (ch == '"')
{
//IF EOF, do NOT continue repeating the last character
if (input -> eof()){break;}

//output Buffer for TEST purposes
cout << strBuffer << endl;

//create new token and push it into the vector
tk = new Token (tkstring, strBuffer, row, col);
tokensUsed.push_back(tk);
startNewString();
}
tokenMatch = true;
}

getNextAndAdd(ch) 转换为

void getNextAndAdd(char ch)
{
ch = nextChar();
strBuffer += ch;
}

我正在给它一些测试数据。我有一个文本文件,我在其中输入了测试数据。问题在于,如果我写例如 "abc""def",它们之间只有一个空格,它将读作 "abc"123,后者是一个数字,完全不同,如果我做两个空格,意思是 "abc""def"然后它会将其读取为 2 个字符串。我还检查了它是否是一个空白,但是后一种有 2 个空格的情况已经识别出最后一个空白实际上是一个空白

知道为什么吗?我尝试删除字符串的最后一个字母说可能是那个错误但它以 "hello

结尾

谢谢

最佳答案

void getNextAndAdd(char ch)
{
ch = nextChar();
strBuffer += ch;
}

您可能认为这会返回您刚刚在 ch 中读取的字符。你会错的。

结果,执行这段代码后:

if (isQuote(ch))
{
//Add it to the Buffer
strBuffer += ch;

do
{
//get Next Character from ifstream and add it to the strBuffer
getNextAndAdd(ch);

ch 包含一个引号,并且两个引号将附加到 strBuffer

关于C++ 省略或添加额外字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23727750/

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