C ++用单位和双位排列间距

时间:2013-12-10 17:18:55

标签: c++ arrays

我正在编写一个洗牌的程序。我得到了它的工作,我想在洗牌前和洗牌后显示卡片。我有一个处理数字10的间距问题。

我知道我可以使用"\t",但还有其他任何方式。

输出摘录

A of Spades
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
J of Spades
Q of Spades
K of Spades

我几乎可以确定导致此问题的是assignDeck(),但我会同时包含这两个问题。

void assignDeck(string *suit, string *cards, string *deck)
{
    int p=0;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<13; j++)
        {       
          deck[p] = cards[j]  + " of " +  suit[i];
          p++;
        }
    }
}
void showDeck(string *deck)
{
    for (int i=0; i<52; i++)
    {
      cout<<deck[i]<<endl;
    }
}

2 个答案:

答案 0 :(得分:2)

由于您将整行输出为一个字符串,因此您可以修改您的卡片串,同时将其添加到卡片组。

void assignDeck(string *suit, string *cards, string *deck)
{
    int p=0;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<13; j++)
        {   
          if ( cards[j].size() < 2 )
            cards[j] = " " + cards[j];
          deck[p] = cards[j]  + " of " +  suit[i];
          p++;
        }
    }
}

编辑:这只是为了对代码进行微小的更改。对于一般情况,我建议您使用std::setw标题中的<iomanip>函数。

答案 1 :(得分:1)