help with function

the program below is suppose to ask the user how many random numbers to print , and then print said amount of random numbers, using functions, but it seems to only be printing one random number, any idea why? thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

void getRandNum( int valuesTotal){

    const int LENGTH = 10;
    int tallyArray[LENGTH] {};
    int randNumber;

    srand(time(0));
    for (int i = 0; i < valuesTotal; i++) {
          randNumber = rand() % 10;

         tallyArray[randNumber]++;

        cout <<  randNumber << "\n\n";

        return;

    }

}


int main () {

    int totalValues;

    cout << "This program analyzes the C++ rand() function.\n\n";
    cout << "Enter the number of random values: ";
    cin >> totalValues;
    cout << "\n" << "Distribution of " << totalValues << " random numbers: \n\n";


       getRandNum(totalValues);



    return 0;
}
Last edited on
Because of the return statement on line 21 it will execute only once. Just remove it and it should work.
okay so i found that the problem was my placement of the return statement, and is now printing the correct amount of numbers, but now my problem is that its only printing numbers from 1-4 and i need it to print numbers from 0 - 9 any input is appreciated thanks

1
2
3
4
5
6
7
8
9
10
11
12
    
rand(time(0));
    for (int i = 0; i < valuesTotal; i++) {
          randNumber = rand() % 10;

         tallyArray[randNumber]++;

        cout <<  tallyArray[randNumber] << "\n";

    }
return;//return should be here instead
}
cout << tallyArray[randNumber] << "\n";
You print only the count of the number not the actual number.
Topic archived. No new replies allowed.