-6

I have a text file that looks like this:

    January 35 45 
    February 45 55 
    etc...

I am trying to read through the file and add each month to a string array and then each following integer into a 2-dimensional array.

I have a months[] array and a temps[][] array.

I'm trying something like this

int size = 0;
while(!file.eof()) {
    file >> months[size];
    size++;
}

I can't figure out how to add the two integers into the int array...

This is for a class, surprise surprise, the requirements are specifically to read the data from the file and insert the month into an array and the two following integers into the two-dimensional array.

We have not gone over structures or vectors yet.

Ben Grzybowski
  • 89
  • 1
  • 2
  • 7
  • 2
    Possible duplicate of [how do i add elements to an empty vector in a loop?](https://stackoverflow.com/questions/17984268/how-do-i-add-elements-to-an-empty-vector-in-a-loop) –  Nov 24 '18 at 17:40
  • 2
    Also: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/q/5605125/2486888) –  Nov 24 '18 at 17:42
  • This would be helpful, https://stackoverflow.com/questions/2084265/reading-integers-from-a-text-file-with-words . – Hiroki Nov 24 '18 at 17:45

2 Answers2

2

Don't use arrays. Model with a structure.

struct Month_Record
{
  std::string month_name;
  int         value_1;
  int         value_2;
};

Next, add a method to input the structure:

struct Month_Record
{
  //... same as above
  friend std::istream& operator>>(std::istream& input, Month_Record& mr);
}

std::istream& operator>>(std::istream& input, Month_Record& mr)
{
  input >> mr.month_name;
  input >> mr.value_1;
  input >> mr.value_2;
  return input;
}

You input becomes:

std::vector<Month_Record> database;
Month_Record mr;
while (input_file >> mr)
{
  database.push_back(mr);
}

You can access the database like an array:

std::cout << database[0].month_name 
          << ", " << database[0].value_1
          << ", " << database[0].value_2
          << "\n";

A nice feature to the model is that you can have the record in one cache line. With parallel arrays, the processor may have to reload the data cache to fetch data from the other arrays (because the entire array may have to be loaded into the cache).

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144
0
int size = 0;
while(size < MAX_ARRAY_LENGTH && // prevent overflow. 
                                 // Will stop here if out of space in array
                                 // otherwise && (logical AND) will require the following be true 
      file >> months[size] // read in month 
           >> temps[size][0] // read in first temp
           >> temps[size][1]) // read in second temp 
{ // if the month and both temperatures were successfully read, enter the loop
    size++;
}

MAX_ARRAY_LENGTH is a constant defining the maximum number of months that can be placed in the arrays.

>> returns a reference to the stream being read so you can chain the operations together and take advantage of the stream's operator bool when done reading. operator bool will return true if the stream is still in a good state.

The logic looks like

loop
if array has room
    read all required data from stream
    if all data read
        increment size
        go to loop.

You may want a test after end of the loop to make sure all of the data was read. If you're reading to the end of the file, something like if (file.eof()) will make sure the whole file was read. If you want a year's worth of data, if (size == ONE_YEAR) where ONE_YEAR is a constant defining the number of months in a year.

user4581301
  • 29,019
  • 5
  • 26
  • 45