I want to preface this by saying this is the first assignment in a 102 csc course. My 101 teacher wasn't the best, so I don't have that good of a foundation.
I have an assignment which requires every action to be in its own function. I have to generate random numbers, then write these numbers to a file, and then a few other things. I have the file created and written to. However, I am having a problem where I can't read the values from the file into an array. They are all still 0 when output to the console. I am sure it is something I'm doing wrong with the functions, but I have no problem taking any criticism. I will include the code below. The function I am working in now is named void read()
.
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <cstddef>
using namespace std;
fstream randomData;
void randomgenerator();
void read(fstream &randomdata);
int main() {
randomgenerator();
read(randomData);
return 0;
}
void randomgenerator() {
srand(time(0));
randomData.open("randomData.txt");
for (int counter = 0; counter < 100; counter++) {
randomData << rand() % 100+1 << endl;
}
}
void read(fstream &randomData) {
int numarray[100] = {};
for (int i = 0; i < 100; i++) {
randomData >> numarray[i];
cout << numarray[i];
}
}
Thank you for taking the time to give input. The std
namespace is used because the professor wants us to. I understand it is not efficient.