reading a table into an array

I have a table in a text file that looks like this:
FICO Cur_Bal
729 39030.46
770 16542.66
815 14093.92
668 37974.53
738 47587.13

my code tries to read the table into a 2D array and convert the numbers into floats.

string Borrowers[6][12];
int main ()
{
ifstream inFile ("test.txt");
for (int i = 0; i < 6; i++){
for (int j = 0; j < 3; j++){
inFile >> Borrowers[i][j];
}
}
cout<<Borrowers[1][0];
cout<<Borrowers[1][1].c_str();
//cout<<atof(Borrowers[1][0].c_str());
return 0;
}

for some reason, cout<<Borrowers[1][1] returns me 7 2 9 (with the extra spaces between the digits), and cout<<Borrowers[1][1].c_str(); returns me 0. what am I doing wrong here?
This almost looks like something a person would export from a speadsheet or database, with column descriptions. Why not export without headings and then do something like this

Text File
729 39030.46
770 16542.66
815 14093.92
668 37974.53
738 47587.13
954 11804.22
101 20117.33


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

using namespace std;

  string Borrower [30];
  double Amt [30];

int main () {
  int X1 = 0;
  
  ifstream inFile ("Test.txt");
  
  while ( !inFile.eof () ) {    
    inFile >> Borrower [X1];
    inFile >> Amt [X1++];
    }
  
  X1--;
  
  for ( int Pntr = 0; Pntr < X1; Pntr++ )
    cout << "  " << Pntr+1 << "  " << Borrower [Pntr] << "   $" << Amt [Pntr] << endl;
      
  return 0;
  }


Result:
  1  729   $39030.5
  2  770   $16542.7
  3  815   $14093.9
  4  668   $37974.5
  5  738   $47587.1
  6  954   $11804.2
  7  101   $20117.3


As the data file changes in size so will your output or at least up to 30 anyway. This example is error prone, but does address the question.
that works, thanks, TightCoder
Last edited on
Topic archived. No new replies allowed.