infile read problem

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
void readOrder( OrderList *&order , ifstream &inFile , int &orderIndex ){
    
    string name = "";
    string item = "";
    string status = "";
    int quan = 0;
    double price = 0.00;

	inFile.open("order.txt");
    if( inFile.is_open() ){
        inFile >> orderIndex;
        
        for( int i = 0 ; i < orderIndex ; i++){
            inFile.ignore();
            getline( inFile , name );
            order[i].setCompanyName(name);
            getline( inFile , item );
            order[i].setItemName( item );
            
            inFile >> price;
            order[i].setPrice( price );
            inFile >> quan;
            order[i].setQuantity( quan );
            
            getline( inFile , status );
            order[i].setOrderStatus( status );
        }
    }
    else
        cout << "Fail to open order list" << endl;
}


main.cpp
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
/* 
 * File:   main.cpp
 * Author: Lim
 *
 * Created on April 2, 2013, 12:48 PM
 */

#include <cstdlib>
#include "Inventory.h"
#include "OrderList.h"

int main(int argc, char** argv) {

    ifstream inFile;
    Inventory *inven;
    OrderList *order;
    int index = 0;
    int orderIndex = 0;
    readInven(inven,inFile,index);
    inven = new Inventory[index];   
    readOrder( order ,inFile , orderIndex );
    
    order = new OrderList[orderIndex+1];
    
    recordStore(inven,index);
    orderItem(inven,order,index,orderIndex);
    return 0;
}


and here is my txt file
1
2
3
4
5
6
7
1
Kezn
Hitachi
HTC2011-HDD-500TB
3
1500
pending


can i ask why my read function cannot read the txt file 1?
You do not initialize *order before calling readOrder, you do that directly afterwards.

Also *& syntax is weird (reference to a pointer?) and unnecessary, just use * as function argument.
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
void readOrder( OrderList *&order , int &orderIndex ){
    ifstream inFile;
    string name = "";
    string item = "";
    string status = "";
	string model = "";
    int quan = 0;
    double price = 0.00;

	inFile.open("order.txt");
	if( inFile.fail() )
		cout << "Unable to open file " << endl;
	inFile >> orderIndex;  
	order = new OrderList[orderIndex+1];
    for( int i = 0 ; i < orderIndex ; i++){
		
        getline( inFile , name );
        order[i].setCompanyName(name);
        getline( inFile , item );
        order[i].setItemName( item );
		getline( inFile , model );
		order[i].setModelName( model );
       
        inFile >> price;
        order[i].setPrice( price );
        inFile >> quan;
        order[i].setQuantity( quan );
        
        getline( inFile , status );
        order[i].setOrderStatus( status );
		
    }
}


why i read file my file output will be like this
1
2
3
4
5
6
7
8
9
10
11
12
13
2

Kezn
Hitachi
0
0

Jye
LG
LG-ST-244/TV
2
11998
pending

the second correct but why first 1 wrong and run
Last edited on
remove the & in OrderList *&order
as what u say . if i remove
how should i initialize at main.cpp there?

 
order = new OrderList;

but my order should have an array after call the readRecord

then can it be initialize 2 time?
No, you'll need to dereference order in the readOrder function like so:
(*order)

Then you can (assuming you have overloaded the [] operator) perform:
(*order)[i].setItemName(...);

Another way is to accept the parameter as OrderList &order
and to pass it whilst dereferencing:
readOrder( *order ,inFile , orderIndex );
If you do this then you do not need to change the indexing code in your readOrder function.
then can i ask
1
2
3
4
5
6
7
8
9
10
11
12
13
2

Kezn
Hitachi
0
0

Jye
LG
LG-ST-244/TV
2
11998
pending

happen because of this problem?
but i don't think so? haha can explain y
solved it.

and
@bour
thanks for your teaching but you are wrong.

i can use *& as my deferencing pointer as well in my function .
i now able to do it. without doing the overload operator..
Topic archived. No new replies allowed.