-2

I'm working on a program where the user will input in the format x,0,0 and it will be saved to a file then read into an array where I can compare the values however I'm stuck when trying to read into a 2D array can someone please help? Think the error is in the struct part This is what I have so far:

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <sstream>
using namespace std;
struct listArray
{
    string name[];
    int price1[];
    int price2[];
};
int main()
{
    listArray la;
    string line;
    cout << "Enter your list: ";

    ofstream fout;
    fout.open("list.txt");
    while (fout) {
        getline(cin, line);
        if (line == "-1")
            break;
        fout << line << endl;
    }
    fout.close();
    int count = 0;

    ifstream listFile;
    listFile.open("list.txt");
    if(listFile.is_open()){
        while (listFile) {
        getline(listFile, la.name[count], ",");
        count++;
        listFile.close();
    }

    }

1 Answers1

0

You can use delimiter to grab the substring, and use this substring to fill your struct, an exemple of split:

#include<iostream>

std::vector<std::string> split(const string& input, const string& regex)
{
  // passing -1 as the submatch index parameter performs splitting
  std::regex re(regex);
  std::sregex_token_iterator first{input.begin(), input.end(), re, -1},last;
  return {first, last};
}

if you are using C++11 or higher, you can use std::vector for arrays, and you also can use boost library, they have nice support for delimiter and other stuffs.