Board index » delphi » Array of char arrays

Array of char arrays

I am trying to read a file with data (date, sales ID, amount) like:

12/27/1999 156-67-9000 450.78
12/29/1999 210-78-6578 953.67
01/13/2001 987-56-4531 211.89

and put the 3 fields into arrays but the dates and the soc sec #s won't go
into my arrays why?

Here is my code:

#include <fstream.h>

const int datemax=11;
const int socsecmax=12;
const int salesmax=7;
const int max = 3;

int main()
{
 char transdate[max][datemax], TempTransDate[datemax];
 char salesid[max][socsecmax], TempSalesID[socsecmax];
 float salesamt[max], TempSalesAmt;

 ifstream infile("sales.dat");

 for(int i=0;i<max;i++)
 {
  infile>>TempTransDate>>TempSalesID>>TempSalesAmt;

  transdate[i][datemax]=TempTransDate[datemax]);  //here is the problem
  salesid[i][socsecmax]=TempSalesID[socsecmax];   //here is the problem
  salesamt[i]=TempSalesAmt;
 }
return 0;

Quote
}

Thanks,

Neo

 

Re:Array of char arrays


On Sat, 24 Feb 2001 15:13:41 -0500, "Neo" <Klink_stalag13@no_spam_yahoo.com>
wrote:

Quote
>char transdate[max][datemax], TempTransDate[datemax];
>char salesid[max][socsecmax], TempSalesID[socsecmax];
>transdate[i][datemax]=TempTransDate[datemax]);  //here is the problem
>salesid[i][socsecmax]=TempSalesID[socsecmax];   //here is the problem

You can't copy C-style arrays by using the assignment operator (=).
You can only do that with C++ string classes which have overloaded
the = operator. Use strcpy() or strncpy() to copy C-style arrays. e.g. -
strcpy(transdate[i], TempTransDate);
or
strncpy(transdate[i], TempTransDate, datemax);

You might benefit from reading ptrtutor.txt at http://www.snippets.org

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Re:Array of char arrays


On Sun, 25 Feb 2001 00:55:18 GMT, wak...@idirect.com (Wayne A. King) wrote:

Quote
>Use strcpy() or strncpy() to copy C-style arrays.

That should read: " ... to copy C-style strings." (nul-terminated char arrays)
To copy C-stlye arrays which do *not* hold C-style strings, use memcpy()
or memmove().

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Other Threads