Applying random numbers to array

Hi!

I'm having some trouble with creating an array with random numbers... How am I to write it?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;

int main()

{
	setlocale(LC_ALL, "");
	int x;
	int end = 1;
	long rows;
	int number [7];
	srand ((unsigned)time (NULL));
	string answer;
		while(end==1)
			{
				cout << "Welcome to the lottery!" << endl;	
				cout << "How many rows did you pay for?? ";
				cin >> rows;
					if(rows < 13)
					{
						cout << "Alright, here's your tip!" << endl;
						while(rows > 0)
						{
							x = 1;
							while(x <= 7)
								{
									int number[] = {rand()%35 + 1};
									cout << number[] << " ";
									x++;
								}
							cout << endl;
							rows--;
						}
		
					}
					else
					{
					cout << "Only 1-12 rows are allowed!" << endl;
					}
					cout << "Continue? (Yes/No) ";

					char temp[256];
					cin >> temp;
					strupr(temp);
					svar = temp;

					if(svar == "YES")
					{
						cout << "Okay!" << endl;
						system("cls");
					}
						else if (answer == "NO" || answer != "NO")
					{
						end = 0;
					}

			}
	system("pause");
	return 0;
}


You see where the rand is on row 32 I want one random number in each of the 7 reserved spaces in number[] in the interval of 1-35 using the rand function. How? I don't understand... Please don't direct me to another website because they don't explain. Any if there's anything with the variables it's my fault because i translated them from Swedish to English so I've might have missed one.
Last edited on
Wrong definition
int number[];

It should have a specific size.
Or get rid of '[]'
(By MB...) :) -> sorry Too short.
@Jackson Marie I know that it's a wrong definition. I've not written anything there because I don't know what I'm supposed to write...
Last edited on
I pretty sure you should get rid of '[]'. Fix the remaining syntax errors, and show everybody your output.
But you want to write all generated values into an array right?
That's right. I get 7 values when i remove the [], but can I still modify them?
Apparently, for each of the rows you want to have 7 random numbers from 1-35.

1
2
3
4
5
6
7
x = 1;
while(x <= 7)
{
	int number[] = {rand()%35 + 1};
	cout << number[] << " ";
	x++;
}


This code should trigger a compiler error because of line 5. A correct version would be:

1
2
3
4
5
6
7
x=0;
while (x < 7)
{
	number[x] = rand()%35 +1;
	cout << number[x] << " ";
	x++;
}
No. This is a temp variable so all data will be destroyed when a program-loop's been finished.
Or use global variable. If you want to access data, you'll need to define an array again.
int array[13][7];

An Ex : For case 13-row, 7-column
Last edited on
int number[7] was defined at line 16 of the whole file

so it's only inaccessible from other functions
however you're right that it should be a 2D array if you wanted to store the data being outputted
Topic archived. No new replies allowed.