c++ oop class problems with reading array

Hi,

i need to read from text file.
this is what inside the text file: (name, code, price)
EG:

Fish F0001 5
chicken C0001 7
beef B0001 6
etc

i need to print it out as a menu. and then add/delete it to/from Order list.
for example i input 1 and 2, it will add chicken C0001 7,and beef B0001 6 into Order list.
and calculate total price.

how can i do that?
Why do people ask questions before googling?????

Look at this, my friend:

http://www.cplusplus.com/doc/tutorial/files/

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void run::runshop()
{
string name;
int count=0;
ifstream infile;
infile.open("products.dat");
if(infile.fail()) cout<<"File not found";
else {
while(infile.peek()!=EOF) {
prods[count].readfile(infile);
count++;
}
}
infile.close();
for(int i = 0 ;i < count ; i++)
{
prods[i].display();
}
int pick;
if (count == 0);
do
{
do
{
cout<<"enter pick";
cin>>pick;
name=prods[pick].getname();
list[pick].storename(name);
count++;
}
while(pick!=99);
}
void product::readfile(ifstream& infile)
{
infile>>name;
}
void product::display()
{
cout<<name;
}
string product::getname()
{
return name;
}
void order::storename(string add)
{
name=add;
}


sorry i am new and here is my code i write to read the file and get the name (only), it look pretty logic to me but it is not doing anything.. where the problem is at?
It doesn't make sense!!!
Why do you need a class to read that file? just use a global function or put it in main directly.
will it still be OO-based if use a global function?

and how do i access the array?
so i can use it at Order class.
like what i try to do at the line 26.

because i need to add any specific product into the Order.
like i want 3 ( cin>>pick ) it will be the 4th item added on the order menu.
Last edited on
OO based doesn't mean you have to blindly make a class. The way you can do this, is by storing the values in a class object. So make a class like this:
1
2
3
4
5
6
7
class Item
{
public:
   std::string name;
   long code;
   double price;
}


And then create instances (objects)of this class to store the data you read from a file. That would be OO.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
   vector<Item> allItems;
   while(//till the end of the file)
   {
      //read file here, then save the data in a class
      Item tempItem;
      tempItem.name = //name from the file;
      tempItem.code  = //code from the file;
      tempItem.price  = //price from the file;
      allItems.push_back(tempItem); //add this item to the array of items
   }

Topic archived. No new replies allowed.