gpt4 book ai didi

c++ - 每 n 个字符打印一个换行符

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:29 26 4
gpt4 key购买 nike

我正在尝试打印一个字符数组,但我想在固定数量的字符后打印一个换行符。到目前为止,我只能在读取第一个固定数量的字符后打印换行符。谁能指出我正确的方向?

    for ( int i = 0; i < (m_nWidth*m_nHeight); i++) {
if (i != m_nWidth)
cout << pMyPointer[i];
else
{
printf("\n");
}
}

最佳答案

if( i % n == 0 && i != 0 ) printf("\n");

其中 n 是换行符之间的字符数。


编辑:在完整的上下文中:

#include <iostream>

int main()
{
const int n = 5;
const std::string str("abcdefghijklmnopqrstuvwxyz");
for (int i=0 ; i<str.size() ; ++i)
{
if (i%n == 0 && i != 0)
std::cout << '\n';
std::cout << str[i];
}
std::cout << std::endl;
return 0;
}

关于c++ - 每 n 个字符打印一个换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339074/

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