The input code doesn't really match the data file.
A product has three attributes:
an ID
a name
a price.
However this structure says that a single product will have
one name, twenty IDs and twenty prices.
1 2 3 4 5 6
|
struct Product
{
string productName;
int productID[20];
double Price[20];
};
|
the getline() here:
getline(objectProduct.productName[c]);
is trying to read a whole line of text into productName[c] which is a single character, it cannot hold more than one letter. It is also wrong as it does not specify that it should use the
infile
stream.
The above issues are related. Rather than the product struct having arrays inside it, there should be an array of products. Like this for example:
1 2 3 4 5 6
|
struct Product
{
string productName;
int productID;
double Price;
};
|
Here's the array:
|
Product Productarray[20];
|
and the input statements would look like this:
1 2 3 4 5 6 7
|
while (infile >> Productarray[c].productID)
{
infile.get();
getline(infile, Productarray[c].productName);
infile >> Productarray[c].Price;
c++;
}
|
When it comes to the search function, pass it the product array,
|
int Search(Product Searcharray[], int sizeofArray, int searchValue)
|
and match the searchValue against the
Searcharray[i].productID
and so on.
I'm getting a sense of
deja vu here, as I already supplied more or less the same code in a previous thread.