0

As an example input file:

02-03-2004,13045634
03-02-2004,16782930

I'm having trouble coming up with code that can read in inputs into different arrays properly. My most recent attempt is this:

int i = 0;
if(inputFile.is_open()){
     while(!inputFile.eof() && i < arraySize){
          getline(inputFile, line, ',');
          date[i] = line;     //array of type string
          getline(inputFile, line, ',');
          deaths[i] = line;     //array of type int
          //...
          i++;
     }
}

I'm struggling to figure out how exactly I'm supposed to move through the input file with the delimiter of ',' while storing everything correctly.

The array I'm trying to read into is a dynamic array, where I already have a function made that determines the size of the array through getline

I also have to keep the ints as ints because there are functions that need to do calculations with them

As a side note, I can't use vectors for this because they haven't been covered in my class yet

Cezlock
  • 19
  • 1
  • What exactly is the problem that you have? What about the code doesn't work? – Hawkeye5450 Jan 20 '22 at 03:12
  • 1
    To begin with: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Jan 20 '22 at 03:13
  • Sigh. Read first, then check for error (or EOF) to break the loop. – Dúthomhas Jan 20 '22 at 03:14
  • I recommend you read *whole* lines, one at a time in your loop. Then use some kind of parsing (for example `istringstream` and `getline(..., ',')`) to extract each field from the line. – Some programmer dude Jan 20 '22 at 03:15
  • I also recommend you use structures to collect the data of each record, and then a vector to collect each record. – Some programmer dude Jan 20 '22 at 03:15

2 Answers2

0

You can do this easily enough, assuming your input is invariably two items on a line, comma-separated:

std::vector<std::string> dates;
std::vector<std::string> IDs;

std::ifstream f( ... );
std::string date, ID;
while (getline( f, date, ',' ) and getline( f, ID ))
{
  dates.push_back( date );
  IDs.push_back( ID );
}

I also wholly recommend Some programmer dude’s comments above: get each line of input and then use a std::istringstream to parse it, and use a struct for your data:

struct Ticket
{
  std::string date;
  std::string job_number;
};

std::vector<Ticket> tickets;

And so on.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
  • I've decided to rewrite my program's variables to be held by a struct, however, I'm still having trouble reading in the ints. I actually have 5 values separated by the comma: date, state, code, incidents, and deaths. I can do getline() with the strings but the command doesn't seem to be able too get integers – Cezlock Jan 20 '22 at 21:43
  • Just use one of the string→int functions C++ has for each input. For example: `int code = std::stoi( code_s );` – Dúthomhas Jan 20 '22 at 23:07
0

Ok, I got it working now. It seems like the problem wasn't the way I was getting the input, but rather the fact that the loop wouldn't run. Turns out that I had to close the file when I was done using it in a different function and then reopen it when it came to the input reading function to reset the while loop's condition.

I got the input working properly with this code after I fixed the loop issue:

void getArrayData(ifstream &inputFile, string *&date, int *&death, string *& state, int *&cas, int arrSize, int *&fip) {
    string temp;
    int k = 0;
    while(getline(inputFile, temp)){
        string workingLine;
        stringstream ss(temp);
        getline(ss, date[k], ',');
        getline(ss, state[k], ',');
        getline(ss, workingLine, ',');
        fip[k] = stoi(workingLine);
        getline(ss, workingLine, ',');
        cas[k] = stoi(workingLine);
        getline(ss, workingLine, ',');
        death[k] = stoi(workingLine);
        k++;
        
    }
    inputFile.close();
}
Cezlock
  • 19
  • 1