gpt4 book ai didi

linux - 终端中的c++输出不会产生新行

转载 作者:太空宇宙 更新时间:2023-11-04 12:35:05 24 4
gpt4 key购买 nike

我有以下问题:

我的 C++ 代码如下所示。

int main()
{
std::string String;

int rows = 0;
int cols = 0;

std::getline (std::cin,String);

std::cin >> rows;
std::cin >> cols;

std::cout << "Enter a fancy string: " << String << std::endl;
std::cout << "Enter the number of rows: " << rows << std::endl;
std::cout << "Enter the number of cols: " << cols << std::endl;

std::string ArrayofStrings;
for(int x = 0; x<rows; x++){
ArrayofStrings = "";
for (int y = 0; y < cols; y++) {
ArrayofStrings += String;
}
std::cout << ArrayofStrings << std::endl; }
// std::cout << std::endl;
return 0;

}

简而言之,它读取字符串并打印出具有指定维度的给定字符串数组。但是当我在 linux 终端上运行它时,输出是这样的:

Enter a fancy string: 4
Enter the number of rows: 5
Enter the number of columns: 5
44444
44444
44444
44444
44444[enkhtaiv@royal-10] (3)$

但是无论如何我都不能让 [enkhtaiv@royal-10] linux 文本从一行开始。我尝试了 std::endl, "\n" 两者,但它们似乎不起作用。

让我困惑的是,这个程序在 IDE 上运行得很好,但是当我在 Linux 终端上编译和运行它时,这个问题仍然存在。我是 C++ 和 linux 方面的初学者,所以如果这是一个完全微不足道的问题,我提前道歉。

最佳答案

解决方案是......添加第二个 EOL。

您没有告诉我们取消注释时会发生什么

 std::cout << std::endl;

在代码的末尾。如果你最终读到了

44444
[enkhtaiv@royal-10] (3)$

如您所料,这可能意味着您的 shell 配置不正确,并且会“吃掉”上一行的 \n。尝试编辑 .bashrc

中的 PS1

关于linux - 终端中的c++输出不会产生新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42152209/

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