0

I have a card deck that will print cards to the terminal one at a time. However because of how the terminal works, they print down vertically. Is there some way or function to get them to print side by side? Here is an example of my code.

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;
Oswald
  • 31,254
  • 3
  • 43
  • 68
user2105982
  • 187
  • 2
  • 6
  • 19

2 Answers2

0

endl inserts a newline character and flushes the output stream. If you want to insert a new line you can use the '\n' character. If you want to flush it (which I doubt you want) you can use std::flush and if you want neither of these two then you do not need std::endl, '\n' or std::flush so you do not use them.

What is the C++ iostream endl fiasco?

Community
  • 1
  • 1
ApplePie
  • 8,814
  • 5
  • 39
  • 60
0

There is nothing built into stream which could help you with printing things side by size but you could represent each card as an array of formatted std::strings and then print the cards side by side by print each row for all of the cards. For example:

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';
}

Obviously, you don't want to format the card from a constant but I wanted to convey the idea rather than getting lost in the details of formatting each card. Of course, you don't want to use std::endl but that's a side show.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380