I have a program that randomly generates an array, and then compares the numbers to the numbers in a csv file. The file is read via getline, where it is then inserted into my struct called 'past_results'.
I then try and insert the struct data into a vector in my main function, but this doesnt seem to work. No data is printed at all. the initial printing of the data works fine and is correctly formatted, but when I send to my vector it wont print from that.
I'm working with a file containing a mix of ints and strings so I was wondering if my data type management was the issue.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
//struct to store data from file
struct past_results {
std::string date;
std::string winningnums;
};
//fills array of size 6 with random numbers between 1-50
void num_gen(int arr[])
{
for (int i = 0; i < 6; i ) {
int num = rand() % 51;
arr[i] = num;
}
}
//function to read csv
void csv_reader(){
std::string line;
past_results results;
past_results res[104];
int linenum = 0;
//open csv file for reading
std::ifstream file("path/to/csv/file", std::ios::in);
if(file.is_open())
{
while (getline(file, line))
{
std::istringstream linestream(line);
std::string item, item1;
//gets up to first comma
getline(linestream, item, ',');
results.date = item;
//convert to a string stream and put into winningnums
getline(linestream, item1);
results.winningnums = item1;
//add data to struct
res[linenum] = results;
linenum ;
}
}
//display data from struct
for(int i = 0; i < linenum; i ) {
std::cout << "Date: " << res[i].date << " \\\\ Winning numbers: " << res[i].winningnums << std::endl;
}
}
int main(){
//random num gen variables
srand(time(NULL));
int size = 6;
int numbers[size];
//fetch and print generated array of nums
num_gen(numbers);
//formatting for terminal output of random nums
std::cout<< std::endl;
std::cout << "Numbers generated: ";
for (auto i: numbers)
std::cout<< i << " ";
std::cout<< std::endl;
std::cout<< std::endl;
//call csv_reader function
csv_reader();
//create vector and insert struct data to it
std::vector<past_results> results;
results.push_back(past_results());
//printing vector of struct
for (int i = 0; i > results.size(); i ){
std:: cout << "Winning nums from struct: " << results[i].winningnums;
}
return 0;
}
CSV format example:
, Date, 1, 2, 3, 4, 5, 6, 7
0, wednesday, 1, 2, 3, 4, 5, 6, 7
1, thursday, 1, 2, 3, 4, 5, 6, 7
etc...
CodePudding user response:
csv_reader
writes to a function local array. Once the function returns all information read from the file is lost. Data is not stored in the class past_result
. The data is stored in an instance of type past_result
.
In main
you push a single element to the vector via results.push_back(past_results());
as a default constructed past_result
contains empty strings you see no ouput.
You should read results into a vector in csv_reader
and return that vector:
std::vector<past_results> csv_reader(...) {
std::vector<past_results> result;
past_results entry;
// ...
results.push_back(entry);
// ...
return result;
}