Text File to a 2D Array

I have a text file that contains a list of IDs and sales amounts. I need to read it into a 2D array. I also need it to accumulate sales for each ID instead of repeating the ID in my array. I can't even get the file read into an array. Here's what I have. What am I doing wrong?

ifstream inFile;
inFile.open(filename);

if (!inFile)
cout << "Error opening file.\n";

for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
inFile >> salesArr[i][j];
cout << salesArr[i][j];
}
}
TwirlingCherries wrote:
What am I doing wrong?

Failing to give us enough code to test.
Here is some of the data from the text file:
254 670.00
689 520.00
691 4800.00
828 3860.00
848 2820.00
229 7848.00
828 607.00
848 820.00
229 815.00
546 1280.00
828 660.00
848 320.00
> I have a text file that contains a list of IDs and sales amounts.
Right, so create a struct
1
2
3
4
struct sales_data {
    int ID;
    double sales;
};


Then you can do this
1
2
3
4
sales_data salesArr[50];
for (int i = 0; i < 50; i++) {
    inFile >> salesArr[i].ID >> salesArr[i].sales;
}
If you are accumulating for the same ID, then use a map<int,double>. No idea why you want a 2-d array.

However, you aren't making clear whether you are failing to find your data file or failing to read it correctly.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <string>
using namespace std;

int main()
{
   const string filename = "salesdata.txt";
// ifstream inFile( filename );
   stringstream inFile( "254 670.00 \n"
                        "689 520.00 \n"
                        "691 4800.00\n"
                        "828 3860.00\n"
                        "848 2820.00\n"
                        "229 7848.00\n"
                        "828 607.00 \n"
                        "848 820.00 \n"
                        "229 815.00 \n"
                        "546 1280.00\n"
                        "828 660.00 \n"
                        "848 320.00 \n" );

   map<int,double> sales;

   int ID;
   double amount;
   while ( inFile >> ID >> amount ) sales[ID] += amount;

   for ( auto p : sales ) cout << p.first << '\t' << p.second << '\n';
}


229	8663
254	670
546	1280
689	520
691	4800
828	5127
848	3960

Last edited on
@lastchance... this is an assignment on 2D arrays, that's the only reason I'm wanting a 2D array. My problem is failing to read the data in correctly.

@salem... I tried the struct but when I run the program I only get 00CFFA44 as out put. I'm not seeing the entire list of IDs and sales figures.
Look, you need to post code showing your attempt and results.
Using the data file contents previously posted:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>

int main()
{
   const std::string filename = "salesdata.txt";
   std::ifstream inFile( filename );

   const int MAXARRAY { 50 };

   double salesArr[MAXARRAY][2] { };

   size_t count { };

   while (inFile >> salesArr[count][1] >> salesArr[count][2]) { count++; }

   double totalSales { };

   std::cout << std::fixed;
   std::cout << std::setprecision(2);

   for (size_t loop { }; loop < count; loop++)
   {
      std::cout << std::setprecision(0) << salesArr[loop][1] << '\t'
                << std::setprecision(2) << salesArr[loop][2] << '\n';

      totalSales += salesArr[loop][2];
   }
   std::cout << '\n';

   std::cout << "Total sales: " << totalSales << '\n';
}

254     670.00
689     520.00
691     4800.00
828     3860.00
848     2820.00
229     7848.00
828     607.00
848     820.00
229     815.00
546     1280.00
828     660.00
848     320.00

Total sales: 25020.00

Personally I would std::pair instead of a struct so just a 1D array is needed.

http://www.cplusplus.com/reference/utility/pair/

And std::vector instead of a C style array so the code can handle a varying amount of data.

http://www.cplusplus.com/reference/vector/vector/
Topic archived. No new replies allowed.