gpt4 book ai didi

c++ - 程序转到换行符,屏幕上没有任何输出

转载 作者:行者123 更新时间:2023-11-30 02:37:25 25 4
gpt4 key购买 nike

我正在研究一个问题,就是将字符串中每个句子的第一个字符大写。例如,如果字符串参数是“你好。我叫乔。你叫什么名字?”程序会返回“你好”。我的名字是乔。你叫什么名字?”我的问题是为什么在我输入要大写的字符串并按下回车后,程序转到换行符并且屏幕上没有任何输出。

这是我的代码:

//Sentence Capitalizer
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;

int main()
{
char argument[1024];
cout<<"Please enter a c string as an agrument and I will capitalizes the first character of each sentence in string: ";
cin.getline(argument, 1024);
argument[0] = toupper(argument[0]);
int i = 0;
while (argument[i+2] != '\0')
{
if (argument[i] == '.')
{
argument[i+2] = toupper(argument[i+2]);
}
i++;
}
cout<<argument[0]<<endl;
return 0;
}

最佳答案

你的bug在于

cout<<argument[i]<<endl;

您已将 i 更新到可能靠近 '\0' 的位置,然后尝试打印它。

修复

退出循环后,只打印argument。或者,如果您想在单独的行中打印每个句子,请记住您应该打印的索引,直到 i

同时 return 0 inside while body 看起来不正确。我想你是想把它放在 while block 之后。

此外来自 this comment of Paolo M ,在 whileif 之后使用分号是您不希望的。

Live example after correction

关于c++ - 程序转到换行符,屏幕上没有任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31604798/

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