C++ 试图使用数组创建一个随机数生成器并获取标识符“i”是未定义的。有人看到问题了吗?

C++ 试图使用数组创建一个随机数生成器并获取标识符“i”是未定义的。有人看到问题了吗?,c++,arrays,random,undefined,identifier,C++,Arrays,Random,Undefined,Identifier,尝试使用数组创建随机数生成器,但a[i]=rand;我的部分代码创建了一个类型标识符i未定义的错误。有人能找到我哪里出了问题吗?谢谢 #include <iostream> #include <string> #include <array> using namespace std; int main() { int a[10] = {}; for (int i = 0; i < size(a); i++); {

尝试使用数组创建随机数生成器,但a[i]=rand;我的部分代码创建了一个类型标识符i未定义的错误。有人能找到我哪里出了问题吗?谢谢

#include <iostream>
#include <string>
#include <array>

using namespace std;


int main()
{
    int a[10] = {};  
  
   
    for (int i = 0; i < size(a); i++); {

        a[i] = rand();

    }

    for (int i = 0; i < size(a); i++) {

        cout << "The random number is: " << a[i] << endl;

    }
    
}

clang很有帮助地指出,在for循环之后有一个意外的分号。

代码中的错误是一个错误的分号。尝试学习像gdb这样的调试器,将有助于解决此类问题。rand函数使用种子,srand用于更改/设置种子

#include <iostream>
#include <string>
#include <array>
#include <ctime>. // Added for random seed generation

using namespace std;


int main()
{
    int a[10] = {};  
  
    srand(time(0)); /* Added this to ensure seed of rand() is always different otherwise you might have ended up with same random numbers on different runs */
    
    for (int i = 0; i < size(a); i++) {   /*Issue was here, you had stray semicolon */

        a[i] = rand();

    }

    for (int i = 0; i < size(a); i++) {

        cout << "The random number is: " << a[i] << endl;

    }
    
}

非常感谢你!我真的很感激。现在很好,谢谢你的帮助
#include <iostream>
#include <string>
#include <array>
#include <ctime>. // Added for random seed generation

using namespace std;


int main()
{
    int a[10] = {};  
  
    srand(time(0)); /* Added this to ensure seed of rand() is always different otherwise you might have ended up with same random numbers on different runs */
    
    for (int i = 0; i < size(a); i++) {   /*Issue was here, you had stray semicolon */

        a[i] = rand();

    }

    for (int i = 0; i < size(a); i++) {

        cout << "The random number is: " << a[i] << endl;

    }
    
}