Hi ive made a code which does two things. First is read from a separate .dat file a product's model number, name a proce. The second bit allows the user to choose the product model. Depending on what the user chooses, only 1 of the products is displayed. So for example if the user inputs 1. Then the program shows information about product 1(model number name price).
here is my code. it works
#include <iostream>
#include <fstream>
using namespace std;
class product
{
public:
int p;
int model;
string item;
int price;
void show();
};
class product2
{
public:
void display();
friend class product;
};
void product::show()
{
product arrayp[10];
ifstream infile;
infile.open("C:\\products.dat");
for(int p=0; p<10; p++)
{
infile
>>arrayp[p].model
>>arrayp[p].item
>>arrayp[p].price;
}
infile.close();
for(int p=0; p<3; p++)
{
cout
<<arrayp[p].model<<" "
<<arrayp[p].item<<" "
<<arrayp[p].price<<"\n";
}
cout<<"choose your product:"<<endl;
cin>>p;
cout<<"You have chosen: "<<endl;
cout
<<arrayp[p].model<<" "
<<arrayp[p].item<<" "
<<arrayp[p].price<<"\n";
}
void product2::display()
{
}
int main()
{
product prod;
product2 pro;
prod.show();
pro.display();
system("pause");
return 0;
}
however i would like to split this into 2 classes. one for reading the items(product) and one for user input and display (product2). i am using friend classes but when i move the "choose product..." over it does not work.
any help is appreciated.
here is the code for products.dat
0 Book 20
1 Pencil 5
2 Pen 10
in short. class product shows all the products. the second class product2 lets user choose one.