gpt4 book ai didi

c++ - 简单的自动换行功能最后一行问题

转载 作者:行者123 更新时间:2023-11-28 03:50:35 25 4
gpt4 key购买 nike

我有一个函数,它接受一个长字符串并将其分成更小的字符串并将它们添加到一个数组中,这样它们就可以输出到一个更漂亮的段落中。它工作正常,除了最后一行不输出。我如何让它输出?这是代码:

void WordWrap(string inputString, string formatedAr[], const int SIZE)
{
unsigned int length;
unsigned int index;
unsigned int word;
unsigned int max = 65;
string outWord;
string outLine;

length = inputString.length();
outWord = "";
outLine = "";
word = 0;

for(int i = 0; i < SIZE; i++)
{
formatedAr[i] = "";
}

for(index = 0; index < length; index++)
{
if(inputString[index] != ' ')
{
outWord += inputString[index];
}
else
{
if(outLine.length() + outWord.length() > max)
{
formatedAr[word] = outLine;
word++;
outLine.clear();
}
outLine += outWord + " ";
outWord.clear();
}
}
}

最佳答案

只有在添加字符会使当前行太长时才写入 formatedAr。当您完成对输入字符串的解析后,您还需要将剩余部分写入 formatedAr

尝试在 for 循环结束后放置 formatedAr[word] = outLine;

关于c++ - 简单的自动换行功能最后一行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5635410/

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