-1

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!
tgol
  • 15
  • 1
  • 1
    Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then [edit] your question to add more details, like what "errors" you get. – Some programmer dude Nov 23 '21 at 17:48

1 Answers1

0

It's fairly easy to read from a file.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main() {
    std::fstream newfile("random-numbers.txt", std::ios::in);
    if (!newfile.is_open()) {
       // Handle file not opening properly
    }
   
    std::vector<int> random_nums;
    std::string data;
    while(getline(newfile, data)){ // Read line by line
        random_nums.push_back(std::stoi(data));
    }
    newfile.close(); // Close the file
}

I use a std::vector here in case you don't know the length of your file. Once you have your vector you can operate on each value or iterate through it however you like.

CyanCoding
  • 1,012
  • 1
  • 12
  • 35