Store Data into Array


I've created a random number generator that places numbers 100-200 into a text file and now I want to store the random numbers from that file into an array.
This is practice specifically using arrays. Any help is greatly appreciated :)



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
#include "pch.h"
#include <iostream>
#include <fstream>



using namespace std;

int main()
{
	ofstream file;		

	file.open("randomData.txt");	


			srand((unsigned)(0)); 
			int ran_data;				

			for (int index = 0; index < 100; index++) {

				ran_data = (rand() % 101) + 100;  
				
			}

		    file << ran_data;   
			file.close();



			int array[101] = {};		
			ifstream is("ran_data");
			int num = 0;
			int x;

			while (num < array[101] && is >> x)
				array[num++] = x;

			cout << "The random numbers are:" << "\n";

			for (int i = 0; i < num; i++) {
				cout << array[i] << '  ';

			}

			is.close();

		return 0;

	}
I think just change it to
 
while (num < 101..;

I could be wrong
mm it's still not printing the numbers, but they're generating in the file though. Just either not storing into the array or printing from the array.
This is what I did
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
#include <iostream>
#include <fstream>



using namespace std;

int main()
{
	ofstream file;		

	file.open("randomData.txt");	


			srand((unsigned)(0)); 
			int ran_data;				

			for (int index = 0; index < 100; index++) {

				ran_data = (rand() % 101) + 100;  
				file << ran_data << endl;
				
			}

			file.close();



			int array[101] = {};		
			ifstream is("randomData.txt");
			int num = 0;

			while (num < 100)
			{
				is >> array[num++];
			}

			cout << "The random numbers are:" << "\n";

			for (int i = 0; i < num; i++) {
				cout << array[i] << ' ';

			}

			is.close();

		return 0;

	}
Thank you so much for taking the time to give me direction on this.
Last edited on
Topic archived. No new replies allowed.