简体   繁体   English

结构的打印向量 - 数据未保存? c++

[英]Printing vector of struct - data not being saved? c++

I have a program that randomly generates an array, and then compares the numbers to the numbers in a csv file.我有一个随机生成数组的程序,然后将数字与 csv 文件中的数字进行比较。 The file is read via getline, where it is then inserted into my struct called 'past_results'.该文件通过 getline 读取,然后插入到我的名为“past_results”的结构中。

I then try and insert the struct data into a vector in my main function, but this doesnt seem to work.然后我尝试将结构数据插入到我的主 function 中的一个向量中,但这似乎不起作用。 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: CSV 格式示例:

, 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...

csv_reader writes to a function local array. csv_reader写入 function 本地数组。 Once the function returns all information read from the file is lost.一旦 function 返回,从文件中读取的所有信息都将丢失。 Data is not stored in the class past_result .数据未存储在 class past_result The data is stored in an instance of type past_result .数据存储在past_result类型的实例中。

In main you push a single element to the vector via results.push_back(past_results());main中,您通过results.push_back(past_results()); as a default constructed past_result contains empty strings you see no ouput.作为默认构造的past_result包含空字符串,您看不到任何输出。

You should read results into a vector in csv_reader and return that vector:您应该将结果读入csv_reader中的向量并返回该向量:

std::vector<past_results> csv_reader(...) {
      std::vector<past_results> result;
      past_results entry;
      // ...
            results.push_back(entry);
      // ...
      return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM