I am trying to do an assignment in which I generate a file with random integer values and then take those values and put them into an array, however, I am not very sure how to do this because I have only been getting errors with how I am trying to do it so I'm wondering if there is a different way to go about doing this task. I have also attached my code and the external text file with the random integers, you might notice there are some other functions but those are just for other requirements in the assignment and to create the txt file.
#include <fstream>
#include <cstdlib>
using namespace std;
//function 1 Create File with rand numbers
void createfile() {
srand(time(0));
int counter = 0;
int generatednum = 0;
ofstream randNums("randomnumbers.txt");
for (int counter = 0; counter < 50; counter++) {
int generatednum = rand() % 105 + 2;
randNums << generatednum << endl;
}
randNums.close();
}
//function 2 count number of apples picked under 25
void countundertwentyfive() {
int under25count;
int under25amount = 0;
ifstream infile;
infile.open("randomnumbers.txt");
if (infile.fail())
{
cerr << "File failed to open:inputfile.txt";
abort();
}
while (!infile.eof()) {
infile >> under25count;
if (under25count < 25) {
under25amount++;
}
}
cout << under25amount << " people picked under 25 apples." << endl;
infile.close();
}
//function 3 count number of apples picked btwn 80 and 100
void btwn80and100() {
int eightyand100apples;
int under80and100amount = 0;
ifstream infile;
infile.open("randomnumbers.txt");
if (infile.fail())
{
cerr << "File failed to open:inputfile.txt";
abort();
}
while (!infile.eof()) {
infile >> eightyand100apples;
if (eightyand100apples >= 80 && eightyand100apples <= 100) {
under80and100amount++;
}
}
cout << under80and100amount << " people picked between 80 and 100 apples." << endl;
infile.close();
}
//function 4 Find average number of apples picked
void averageapplepicked() {
int numbergetter;
int sum = 0;
ifstream infile;
infile.open("randomnumbers.txt");
if (infile.fail())
{
cerr << "File failed to open:inputfile.txt";
abort();
}
while (!infile.eof()) {
infile >> numbergetter;
sum += numbergetter;
}
sum /= 50;
cout << "Approximately " << sum << " apples were picked on average" << endl;
infile.close();
}
//Use of previous functions along with the array thing I dont understand how to do
int main() {
createfile();
countundertwentyfive();
btwn80and100();
averageapplepicked();
int arrayvalues;
int arraydisplay;
ifstream infile;
infile.open("randomnumbers.txt");
if (infile.fail())
{
cerr << "File failed to open:inputfile.txt";
abort();
}
while (!infile.eof()) {
int arraydisplay[50] = { infile >> arrayvalues };
}
cout << arraydisplay[50] << endl;
infile.close();
return 0;
} ```
The txt file generated by the code
3
74
89
20
35
79
12
12
82
98
25
5
59
99
54
46
53
38
98
42
103
55
73
96
98
75
48
42
70
82
50
62
26
90
17
103
24
56
93
46
10
49
33
76
40
44
6
101
43
43
Thank you so much for any help!