【发布时间】:2015-02-20 19:36:13
【问题描述】:
首先,我是一个早期的初学者程序员,希望得到一些帮助。
我已经编写了以下代码,这些代码是我测试过的:
- 5 个随机数介于:1 到 39 //num1gen 到 num5gen - 例如 A 组
- 1 到 14 之间的随机数 //Thunderball - 例如 B 组
我只想按升序计算
我仍然需要将生成的随机数放入如下所示的整数名称中:
即
- num1gen = 12
- num2gen = 24
- num3gen = 3
- num4gen = 5
- num5gen = 32
- 雷球 = 12
错误:ISO C++ Forbids deceleration of 'i' with no type [-fpremissive] 在第 16 行。
到目前为止,我在以下用户的帮助下完成了代码:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
srand(time(NULL));
std::vector<int> numA(5);
srand( time(NULL) );
for( auto i(0); i < numA.size(); ++i ) //line no 16 error
numA[i] = (rand()%49+1);
int num1gen=(rand()%49+1); // this is the value of ball no.1
int num2gen=(rand()%49+1); // this is the value of ball no.2
int num3gen=(rand()%49+1); // this is the value of ball no.3
int num4gen=(rand()%49+1); // this is the value of ball no.4
int num5gen=(rand()%49+1); // this is the value of ball no.5
std::sort(numA.begin(), numA.end());
num1gen=numA[0];
num2gen=numA[1];
num3gen=numA[2];
num4gen=numA[3];
num5gen=numA[4];
cout<<num1gen<< ", "<<num2gen<< ", "<<num3gen<< ", "<<num4gen<< ", "
<<num5gen<< " ";"
return 0;
}
【问题讨论】:
-
还有什么问题?
-
不要在循环中使用
srand()。 -
使用vector 存储所有数字,然后对其进行排序。
-
只需将您的数字读入数组或
std::vector并在输出前对其进行排序。 -
如果我将这些数字读入一个数组,我仍然可以将生成的随机数放在它们唯一的整数名称中吗?即 num1gen、num2gen 等。仍然持有数字。因为我的程序的其余部分正在检查这些内存名称位置。
标签: c++ random integer srand time.h