0

I have a text file with six Product Codes, Product Names and Product Prices, grouped per line. Each line has a diferrent product. I used for and fin to read data. Now I want to add those data to an array.

The product codes are set as pcode, product names as pname, product prices as pprice and the array as arrP[]. I'm told to add those data to an array using setData, for which I have a constructor in class Product.

Data reading works. However I cannot understand how to use setData to add those to arrP[]. I've tried arrP[i].setData(pcode, pname, pprice); in the same for that reads the data but it's obviously wrong because it crashes when it's about to execute that line. Below I quote my code about this part.

This is the code of the function that reads the data and moves it to the array (N is defined as 6):

int addptoarray(ifstream &fin, Product arrP[])
{
  Product p;
  int pcode;
  int i;
  char pname[21];
  float pprice;

  n = 0;


  for(i=0; i<N; i=i+1){

  fin >> pcode;

  fin.get(pname, 21);

  fin >> pprice;


  arrP[i].setData(pcode, pname, pprice);


  n = n+1;

  }


}

This is the part on main that triggers the function (N is defined as 6):

[...]
addptoarray(infile, arrP[N]);
[...]

This is the code of the constructor for setData in Product class:

void Product::setData(int pcode1, char pname1[], float pprice1)
{
pcode = pcode1;
pname[21] = pname1[21];
pprice = pprice1;
}
roschach
  • 8,390
  • 14
  • 74
  • 124
Xfighter
  • 1
  • 1
  • 2
    I would recommend to read some [good book about C++](https://stackoverflow.com/q/388242/580083). It seems to me that you have not much knowledge about how arrays work in C++. Also, than "plain C" arrays, using some container from C++ standard library (such as `std::vector`) is usually a better option. – Daniel Langr Jan 16 '19 at 11:11
  • what i usually do if i have to read data with specified text format: 1) make struct which corresponds for text format and operator >> for reading from stream 2) use std::copy to read content of file into back_inserter of vector of struct from (1) . So basically you code shrinks to struct (you need it anyway) and 1 line of actual reading from file. If you need to do something that can be done by algorithm from standard library - it usually best way to do it (leaves no room for errors and easy to read/understand for others) – Andrew Kashpur Jan 16 '19 at 11:17
  • `Product::setData` cannot be a constructor: a constructor must have the same name of the class. – roschach Jan 16 '19 at 11:30
  • Are you sure it doesn't crash because of a seg fault? Are you properly initializing the arrP array? I think we need to check out your entire code to make sure. – Greg K. Jan 16 '19 at 11:34
  • Please add some information about crashing. It might crash also because you are reading more elements than the ones stored. `addptoarray(infile, arrP[N])` is reading the `N+1` element. – roschach Jan 16 '19 at 11:36
  • It exits when it tries to execute that line with return value 3221225477 – Xfighter Jan 16 '19 at 11:48
  • Also notes: in your function you are not returning any value. Plus you are defining the function as `int addptoarray(ifstream &fin, Product arrP[])` which is expecting an array but you are calling it `addptoarray(infile, arrP[N]);`: can you try `addptoarray(infile, arrP)`;? – roschach Jan 16 '19 at 11:51

1 Answers1

1
char pname[21];

Okay, pname is an array with 21 entries.

void Product::setData(int pcode1, char pname1[], float pprice1)
{
    pcode = pcode1;
    pname[21] = pname1[21];
    pprice = pprice1;
}

That middle line doesn't make any sense. The middle line tries to set the value of the 22nd entry in pname. Why? Because pname[0] is the first entry in pname and pname[1] is the second entry. So pname[21] = ... tries to set the 22nd entry in pname to something.

But pname only has 21 entries. Trying to set its 22nd entry to something is an access to the array out of bounds.

If you're expecting pname to be a string, you have no code that reflects that. It's not actually a string type (C++ has those) and you don't use C-style string function (like strcpy) to manipulate it. So it's not going to act like a string at all.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278