Hello everyone.
I am having some issues with my homework assigment. they are asking me to generate a list of random numbers; no more than 50 (range 1-10). The user(s) need to specify how many he wants in the list. I tried using an array, but it didn't work out as i expected.
int main()
{
const int SIZE = 50;
int Append[SIZE];
for (int &val : Append)
{
cout << " How many random numbers : ";
cin >>val;
for (int val : Append)
cout << val << endl;
system("pause");
return 0;
The output of this code gives me 50 random numbers instead of the specific amount that I specify. :l
When posting code, please use code tags. Highlight the code and click the <> button to the right of the edit window. This shows the code with line numbers and syntax highlighting.
Here is your code with tags and indented to reflect the structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
constint SIZE = 50;
int Append[SIZE];
for (int &val : Append)
{
cout << " How many random numbers : ";
cin >>val;
for (int val : Append)
cout << val << endl;
system("pause");
return 0;
Notice that the code that prompts for the number of random numbers (lines 11-12) are inside a for loop. You don't want to prompt 50 times, so that's not right. I suggest that you just remove the loop at lines 9-10.
You are also using "val" for two different things: is it the number of random numbers, or is it an individual random number? You need two different variables to represent these two different concepts.