如何在C ++中随机数生成的应用程序中的数字之间建立空格?

时间:2012-01-31 23:33:51

标签: c++

我需要能够在每个数字之间出现空格。这是我的代码。任何帮助都是极好的!这个应用程序允许您为1到49之间的insta选择号生成6行6个数字,它必须选择两行6个数字,1 - 49用于扭曲,1行用6个数字用于标记。

    #include <iostream> 
    #include <iomanip>
    #include <cstdlib>
    using namespace std;

    int main()
    {
    {
cout << "*** LOTTO  MAX  INSTA  PICK ***" << endl;
cout<< " " << endl << endl;
    }

    {
cout << "Your Insta Pick Numbers" << endl;
cout<< " " << endl << endl;
    }
for (int counter = 1; counter <= 24; ++ counter)
{

    cout << setw(1) << (1 + rand() % 49);


    if (counter % 6 == 0)
        cout << endl;
}
{
    cout<< " " << endl << endl;
    cout<< " " << endl << endl;
}
{
cout << "Your Twist Numbers" << endl;
cout<< " " << endl << endl;
    }
for (int counter = 1; counter <= 12; ++ counter)
{

    cout << setw(1) << (1 + rand() % 49) , "  ";


    if (counter % 6 == 0)
        cout << endl;

}
{
    cout<< " " << endl << endl;
    cout<< " " << endl << endl;
}


{
cout << "Your Tag Numbers" << endl;
cout<< " " << endl << endl;
    }
for (int counter = 1; counter <= 6; ++ counter)
{

    cout << setw(1) << (1 + rand() % 12);


    if (counter % 6 == 0)
        cout << endl;

}
{
    cout<< " " << endl << endl;
   cout<< " " << endl << endl;
}
{
cout << "Thank you for playing!! please check ticket a year minus a day               from date of purchase" <<endl;
}
    };

2 个答案:

答案 0 :(得分:2)

你做的时候几乎已经拥有了

cout << setw(1) << (1 + rand() % 49) , "  ";

但这不符合你的想法。它评估两个表达式,用逗号分隔 - cout << setw(1) << (1 + rand() % 49)" "。第一个执行setw并打印(1 + rand() % 49),第二个执行自我评估并且无效。请注意<<cout的输出运算符,因此您只需将逗号更改为<<

cout << setw(1) << (1 + rand() % 49) << "  ";

对于您打印数字的其他地方也是如此。

答案 1 :(得分:1)

在循环中使用cout << setw(1) << (1 + rand() % 49) << " ";。 (请注意,,已替换为<<