Returning multiple arrays from a text file

I have a file where I need to get the first column of text and return it as an array and the rest of the file into a separate array.
The text file looks like:
Takoma_store 2.70 2.70 71.30 3.60 14.70 17.40 23.90 71.30 51.20 31.20 2.70 96.20
Bethesda_store 12.70 6.80 8.90 6.80 17.80 24.60 7.90 8.90 18.30 6.80 6.80 72.90
Arlington_store 22.50 1.40 17.60 1.40 32.50 33.90 2.70 17.60 4.80 1.40 1.40 89.40
Nashville_store 2.60 9.40 56.70 2.10 3.60 13.00 6.80 56.70 23.40 6.80 9.40 71.50
Springfield_store 9.60 14.10 8.30 3.15 6.80 16.20 1.40 8.30 3.70 1.40 9.40 87.25
Baltimore_store 6.50 21.15 8.90 2.73 89.40 98.15 9.40 8.90 9.80 6.80 8.75 110.88
Aurora_store 9.40 31.73 8.20 2.09 71.50 80.66 9.40 8.20 67.80 1.40 9.16 146.31

Everywhere there is a store name is a new line.

This is my code so far... it gets the first column and of store names and stores it into an array but the rest of the file needs to be placed inside of an array with 7 rows and 12 columns, can someone guide me on where to go next.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
 ifstream infile("Data.txt");
 infile >> Name;
 int i,j;
 if (infile.is_open())
 {
  string line;
  string Stores[7];
  float Sales[7][12];
  for (int i = 0; i <7; ++i)
  {
   if (getline(infile, line))
   {
    Stores[i] = Name;
    cout << Stores[i] << endl;
    infile >> Name;
   }
  }
 }
 return 0;
}

By "array" you mean "2D array" / "matrix" / "table".

What do you need the data for? What operations will you do with the data?

How about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Store {
  std::string name;
  std::vector<double> sales;
};

// Then you can read in 
std::vector<Store> stores;
Store entry;
for (int i = 0; i <7; ++i)
{
  // read from one line of file into the entry

  stores.emplace_back( entry );
}


You do already #include <sstream> .
Can we presume that you know why? (Based on your code, I guess not.)
On your other thread http://www.cplusplus.com/forum/beginner/269288/ (on apparently same program -- double-posting is not good, BTW) you have seen stringstream.
std::cin is a stream.
ifstream is a stream.
istringstream is a stream.
See http://www.cplusplus.com/reference/sstream/istringstream/istringstream/
You are using so many C++-isms I admit I do not know why you are using an array over a std::vector and some kind of class to represent your store and sales.

Nevertheless, the trick is that everything you repeat must be in its own loop.
Also, do not loop on EOF.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  // The data we want
  const int MaxStores = 100;
  string StoreNames[MaxStores];
  float MonthlySales[MaxStores][12];
  int NStores = 0;

  // The file full of stuff we want
  ifstream infile("Data.txt");

  // Stuff we need to get it
  string store_name, sales_string;

  // Loop while we get something useful from the file
  while (getline( infile >> store_name, sales_string ))
  {
    // Make sure there is room
    if (NStores >= MaxStores)
    {
      std::cerr << "Too many stores!\n";
      break;
    }

    // Get the store name
    StoreNames[NStores] = store_name;

    // Get the store's monthly sales
    istringstream sales_stream( sales_string );
    float sales;
    for (int month = 0; month < 12; month++)
    {
      sales_stream >> MonthlySales[NStores][month];
    }

    // Update the number of stores we have decoded
    NStores += 1;
  }

  // do stuff with the stores here 

Hope this helps.
Last edited on
Yes this helps a lot thank you! We have not yet learned vectors in my class and I do not want to use something we have not learned in my program yet. I do still have another question, how do I know that the arrays are working? Like for example can I print StoreNames[NStores] and have it show the names of the stores.
NStores will always be the number of stores. Since arrays start at zero, StoreNames[NStores] will not have useful information.

This is the idea:
1
2
3
const int MAX_XS = 100; // maximum number of xs
int xs[MAX_XS]; // room for that many xs
int num_xs = 0; // currently no x exists in xs 

Now to add an x to your list of xs:
1
2
xs[num_xs] = -7;  // update the first available (unused) x
num_xs += 1;     // add it to our count 

Hope this helps.
Last edited on
Topic archived. No new replies allowed.