2013-11-23 141 views
0

我有一张卡片卡,可以一次将卡片打印到终端上。但由于终端的工作原理,它们会垂直打印。是否有某种方式或功能让他们并排打印?这是我的代码的一个例子。并排打印卡座

cout << "---------" << endl; 
cout << "|"<<"6"<<setw(7)<<"|"<<endl; 
cout << "|"<<setw(4)<< "S" << setw(6)<<"S"<<setw(2)<<"|"<<endl; 
cout << "|"<<setw(8)<<"|"<<endl; 
cout << "|"<<setw(8)<<"|"<<endl; 
cout << "|"<<setw(4)<< "S" << setw(6)<<"S" <<setw(2)<<"|"<<endl; 
cout << "|"<<setw(8)<<"|"<<endl; 
cout << "|"<<setw(8)<<"|"<<endl; 
cout << "|"<<setw(4)<< "S" << setw(6)<<"S"<<setw(2)<<"|"<<endl; 
cout << "|"<<setw(7)<<"6"<<"|"<<endl; 
cout << "---------" << endl; 
+4

你知道'endl'会将你移动到下一行,对吗? – nhgrif

回答

0

endl插入换行符并刷新输出流。如果你想插入一个新行,你可以使用'\ n'字符。如果你想刷新它(我怀疑你想要),你可以使用std :: flush,如果你不想要这两个,那么你不需要std :: endl,'\ n'或std :: flush,所以你可以不使用它们。

What is the C++ iostream endl fiasco?

0

没有什么内置流,其可以通过尺寸帮助您与承印物的一面,但你可以表示每个卡的阵列格式std::strings,然后并排通过每个打印打印卡面排所有的牌。例如:

class card { 
public: 
    std::string get_row(int row) const { 
     switch (row) { 
      case 0: case 10: return "---------"; 
      case 1: return "|6  |"; 
      // ... 
     } 
    } 
    // ... 
}; 
std::vector<card> deck; 
// fill the deck 
for (int i(0); i != 11; ++i) { 
    for (auto const& card: deck) { 
     std::cout << card.get_row(i); 
    } 
    std::cout << '\n'; 
} 

显然,你不想格式化从一个恒定的卡,但我想传达的理念,而不是格式化每张卡的细节迷路。当然,你don't want to use std::endl,但这是一个侧面表演。

+0

嗯..有趣。我将不得不更多地考虑这一点。 – user2105982