2017-02-10 557 views
0

我有以下问题:C++终端输出不生产新行

我的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[[email protected]] (3)$ 

但我不能作出这样的[enkhtaiv @皇家-10]从线Linux文本开始不管。我试过std::endl, "\n",但他们似乎没有工作。

令人困惑的是,这个程序在IDE上工作得很好,但是当我在Linux终端上编译并运行它时,这个问题仍然存在。 我是C++和linux的初学者,所以如果这是一个完全无关紧要的问题,我会提前道歉。

回答

0

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

你没有告诉我们发生了什么,当你在你的代码的末尾取消注释

std::cout << std::endl; 

。如果你终于结束了读取

44444 
[[email protected]] (3)$ 

您的预期,这可能意味着你的shell配置不正确,并且将“吃”前行的\n。尝试编辑您的.bashrc

中的 PS1