lundi 2 septembre 2019

I work at a program that generate random numbers and I want to store the numbers in an array

I started learning C++ yesterday and now I try to create a program that generate random numbers that should be stored in an array. The problem is that it store only the last number generated. This is my code (sorry if it looks awful - and probably it really is):

int num = 0;
int i = 0;
int generatedNumbers[10];

int main()
{
    srand(time(NULL));
    num = rand() % 10; 
    generatedNumbers[i] = num; 
    if (num != 0) {
        cout << num << endl;
        return choice();
    } else {
    main();
    }
}

int choice() {
   [.....USELESS STUFF....]
                cout << generatedNumbers[i] << endl;
}

So let's say the program generate the numbers: 3, 1, 8, 9. If I wanna see the numbers stored in generatedNumbers, the only number that appears is the last one (in this case, 9) and it appears four times (in this particular case). I'm pretty sure is very easy to solve the problem but honestly I kinda lost my patience. Thanks alot!




Aucun commentaire:

Enregistrer un commentaire