item index problem

Pages: 12
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
void readOrder( OrderList *&order , int &orderIndex , int &itemIndex ){
    ifstream inFile;
    string name = "";
    string item = "";
    string status = "";
    string model = "";
    char* date;
    date = new char;
    int quan = 0;
    double price = 0.00;
    double warranty = 0.00;
    
    inFile.open("order.txt");
    if( inFile.fail() )
            cout << "Unable to open file " << endl;
    inFile >> orderIndex;  
    order = new OrderList[orderIndex+1];
    inFile.ignore();
    for( int i = 0 ; i < orderIndex ; i++){
		
        getline( inFile , name );
        order[i].setCompanyName(name);
        
        inFile >> itemIndex;
        inFile.ignore();
        for( int k = 0 ; k < itemIndex ; k++ ){
        
            getline( inFile , item );
            order[i].orderItem[k].itemName = item;
            getline( inFile , model );
            order[i].orderItem[k].modelName = model;

            inFile >> quan;
            order[i].orderItem[k].quantity = quan;
            inFile >> price;
            order[i].orderItem[k].price = price;
            inFile >> warranty;
            order[i].orderItem[k].warranty = warranty;
            inFile.ignore();
        }
        inFile.getline(date,9);
        order[i].setDate(date);
        getline( inFile , status );
        order[i].setOrderStatus( status );
		
    }
}

void saveOrder(  OrderList *&order , ofstream &outFile , int &orderIndex ,int &itemIndex){
    outFile.open( "order.txt" );
    outFile << orderIndex << endl;
    for( int i = 0 ; i < orderIndex ; i++ ){
        outFile << order[i].getCompanyName() << endl
                << itemIndex << endl;
        for( int k = 0 ; k < itemIndex ; k++ ){
            outFile << order[i].orderItem[k].itemName    << endl
                    << order[i].orderItem[k].modelName   << endl
                    << order[i].orderItem[k].quantity    << endl
                    << order[i].orderItem[k].price       << endl
                    << order[i].orderItem[k].warranty    << endl;
        }
        outFile << order[i].getDate()        << endl;
        outFile << order[i].getOrderStatus() << endl;
	}
    outFile.close();
}

void saveOrderTrack( OrderList *&order , ofstream &outFile , int &orderIndex ,int &itemIndex){
    outFile.open("tracking.txt");
    outFile << orderIndex << endl;
    for( int i = 0 ; i < orderIndex ; i++ ){
        outFile << order[i].getCompanyName() << endl
                << itemIndex << endl;
        for( int k = 0 ; k < itemIndex ; k++ ){
            outFile << order[i].orderItem[k].itemName  << endl
                    << order[i].orderItem[k].modelName << endl
                    << order[i].orderItem[k].quantity  << endl
                    << order[i].orderItem[k].warranty  << endl;
        }
        outFile << order[i].getDate() << endl;
    }
    outFile.close();
}

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
void orderItem( Inventory *& inven , OrderList *& order , int &index , int &orderIndex , int &itemIndex ){
    system( "cls" );
    displayItem(inven,index);

    bool matchItem     = false;
    bool again         = false;
    bool matchQuantity = false;
    bool matchAll      = false;
    
    string status = "";
    string name   = "";
    string item   = "";
    string model  = "";
    char date[9];
    double price  = 0.00;
    double warranty = 0.00;
    int quan = 0;
	itemIndex = 0;
    
    char choice = 'a';
    char continueBuy ='a';
    ifstream inFile;
    ofstream outFile;
    
	cin.ignore();
     cout << "Enter company name : ";
     getline( cin , name );
     stringtoUpper( name );
     order[orderIndex].setCompanyName( name );

     do{
        cout << "Do you want add an order [y / n] : ";
        cin  >> choice;
        switch( choice ){
            case 'y':
            case 'Y':
                do{
                    cin.ignore();
                    do{
                        do{
							if( itemIndex == 0 ){}
							else{
								itemIndex++;
							}
                             cout << "Enter item name : ";
                             getline( cin , item );
                             stringtoUpper(item);

                             for( int i = 0 ; i < index ; i++ ){
                                 if( item != inven[i].getItemName() )
                                     matchItem = false;
                                 else{
                                     matchItem = true;
                                     break;
                                 }
                             }
                             if( !matchItem )
                                 cout << "Item Does not exist!" << endl;
                         }while(!matchItem);
                         order[orderIndex].orderItem[itemIndex].itemName = item;
                     
                         for( int i = 0 ; i < index ; i++ ){
                             if( item == inven[i].getItemName() ){
                                 if(inven[i].getQuantity() <= 0){
                                     cout << "Item sold out ! Need to wait stock" << endl;
                                     matchAll = false;
                                 }
                                 else
                                     matchAll = true;
                             }
                         }
                         if( matchAll == true ){
                             do{
                               cout << "Enter quantity : ";
                               cin  >> quan;
                               while( cin.fail() ){
                                   cin.clear();
                                   cin.ignore(100,'\n');
                                   cout << "You cannot enter character , only number" << endl;
                                   cout << "Enter quantity : ";
                                   cin  >> quan;
                               }
                                for( int i = 0 ; i < index ; i++ ){
                                    if( item == inven[i].getItemName() ){
                                        if( quan > inven[i].getQuantity()){
                                            cout << "Invalid input ! Item quantity exceed" << endl;
                                            matchQuantity = false;
                                        }
                                        else{
                                            int remainQuantity = inven[i].getQuantity();
                                            remainQuantity -= quan;
                                            inven[i].setQuantity(remainQuantity);
                                            matchQuantity = true;
                                        }
                                    }
                                }
                             }while( matchQuantity == false);

                            order[orderIndex].orderItem[itemIndex].quantity = quan;

                            for( int i = 0 ; i < index ; i++ ){
                                if( item == inven[i].getItemName() ){
                                    price = quan * inven[i].getPrice();
                                    model = inven[i].getModel();
                                    warranty = inven[i].getWarranty();
                                }
                            }
                            cout << "Total Price : RM" << price << endl;
                            order[orderIndex].orderItem[itemIndex].price = price;
                            order[orderIndex].orderItem[itemIndex].modelName = model;
                            order[orderIndex].orderItem[itemIndex].warranty = warranty;

                            status = "pending";
                            stringtoUpper(status);
                            order[orderIndex].setOrderStatus(status);


                            itemIndex++;//to increase 1 array, if not array will not be save and output error
                            
                            displayItem(inven,index);
                            
                            cout << "\n\nDo you still want to continue [y/n] ? : ";
                            cin  >> continueBuy;
                            do{
                                switch( continueBuy ){
                                    case 'y':
                                    case 'Y':
                                        again = true;
                                        break;
                                    case 'n':
                                    case 'N':
                                        again = false;
                                        _strdate(date);
                                        order[orderIndex].setDate(date);
                                        orderIndex++;
                                        saveOrder(order,outFile,orderIndex,itemIndex);
                                        saveOrderTrack(order,outFile,orderIndex,itemIndex);
                                        saveInventory(inven,index);
                                        break;
                                    default:
                                        cout << "Invalid input !" << endl;
                                        break;
                                } 
                            }while( choice != 'Y'&&choice != 'y'&&choice != 'n'&& choice!='N' );
                         }
                     }while( matchAll != true );
                }while(again == true);
                break;
            case 'N':
            case'n':
                cout << "You cancel order item" << endl;
                break;
            default:
                cout << "Invalid input ! Please re-enter" << endl;
                break;
        }
    }while( choice != 'Y' && choice != 'y' && choice != 'N' && choice !='n');
}
at start my input was correct as here is my order.txt file
1
2
3
4
5
6
7
8
9
10
1
KEZN
1
HITACHI
HTC2011-500TB
2
1000
4
04/11/13
PENDING

but when i insert with different company and i buy item. my order.txt will become like this
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
2
KEZN
3
HITACHI
HTC2011-500TB
2
1000
4


0
0
0


0
0
0
04/11/13
PENDING
LIM
3
HITACHI
HTC2011-500TB
2
1000
4


0
0
0
SAMSUNG
ST-56/2-PLASMA
2
9000
6
04/11/13
PENDING
well, your function are way to vast. For the future you should separate it in smaller function. So that you understand better what happens in your program. Passing everything as a reference is a bad habit too.

your main problem is again that you get out of bounds.
On line 17 in readOrder() you create an OrderList with as many items as stored + 1, but that is not enough.

I suggest to remove line 17. Instead make a rather large array (10000 or so) at the beginning of your prgram that is able to keep all orders.

Or you keep line 17 and add 10000 instead of 1.
That should remove the out of bounds problem.

What about order[...].orderItem? Does it have the 'out of bounds' problem as well?
i know where is my problem.
all still going fine.
the problem is

my int itemIndex that always pass with reference.
so when i enter order for continously.
the error might happen

if i only order 1 item it's fine

if continue buy 2 times.
the error will be come out as i show u.
if continue buy 2 times.
the error will be come out as i show u.
Yes, the reason you will find on line 17:
order = new OrderList[orderIndex+1]; // This is the reason why you can buy only 1 item

if you replace the +1 with +10000 you will be able to buy 10000 items
i get what you mean .
for example.

i buy the first item
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enter company name : kezn
..
..
..
do you wan to continue buy [y/n] : n
//pop to main menu again
//so i re-enter to do orderFunction
enter company name : kezn
..
..
..
do you wan to continue buy [y/n] : y
enter item : ...
..
..
do you wan to continue buy [y/n] : n
press any key to continue...

//after wards when i display order record.
//it will be 


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
2
KEZN
3
HITACHI
HTC2011-500TB
2
1000
4


0
0
0


0
0
0
04/11/13
PENDING
LIM
3
HITACHI
HTC2011-500TB
2
1000
4


0
0
0
SAMSUNG
ST-56/2-PLASMA
2
9000
6
04/11/13
PENDING


even i change to 10000..
can help me ? just small problem i think.
or might i upload the cpp for u?
the orderIndex is just looping to find the company.
but the problem i had is itemIndex .?
It is possible that itemIndex is a problem
This doesn't look right:
1
2
3
4
							if( itemIndex == 0 ){}
							else{ // if 0 you want 0, if 1 you want 2? why?
								itemIndex++;
							}

I don't know anything about orderItem

It doesn't seem right that you increase orderIndex on line 135 only when your done with buying.

It's certainly not a trivial problem, there're too many lines involved.
If you cannot solve the problem this way you need to provide allt the code.
http://www.2shared.com/file/V11efXXq/Assignment_1.html

here is the link.
actualy it's just a small problem

i know my skil is not a good habit but at least i can know how it run..
will try improve myself again after solve this problem..

mind help ? sorry ya
well, so I checked your program. You're right itemIndex is the problem.

You must not pass it as a paramter to orderItem().
Instead you need itemCount as a member of OrderList. It is the current number of item within the order.
read/write it from/to the file.

itemIndex will be a local variable in orderItem(). Initialize it with 0
did you mean that i have to add a data member itemIndex inside the stucture of
1
2
3
4
5
struct item{
     ...
     ...
     itemIndex
}


isn't?
i also think like that but don't know it's really working or not.
then when i pass the variable how should i pass to it?
can write a code at here?

1
2
3
enter item name : 
getline( cin , item );
order[orderIndex].orderItem[tempIndex].itemName = item;


so i have to delete the parameter itemIndex at orderItem()
right?
and how should i use the itemIndex and itemCount?
int itemCount is basically do that what i write for the file right?
int itemIndex is use for what?
Last edited on
isn't?
No, do it like I said:
1
2
3
4
5
6
7
8
9
10
11
12
class OrderList {
public:
    OrderList(); // Note: Don't forget to set itemIndex -> 0

...    
    
private:
    int itemIndex; // Note: new member
    string companyName;
    string orderStatus;
    char *date;
};


so i have to delete the parameter itemIndex at orderItem()
right?
Yes

can write a code at here?
1
2
3
4
5
enter item name : 
getline( cin , item );
order[orderIndex].orderItem[order[orderIndex].itemIndex].itemName = item;
order[orderIndex].itemIndex++; // Note: You must ensure that the user cannot add more than 5 items
// Otherwise your program will crash 


int itemCount is basically do that what i write for the file right?
int itemIndex is use for what?
It's only different names for the same thing.
I'd rather name it itemCount, but itemIndex is ok too.

Every member of OrderList must be read/written from/to file:
1
2
3
    for( int i = 0 ; i < orderIndex ; i++ ){
        outFile << order[i].getCompanyName() << endl
                << order[i].itemIndex << endl;



Your output looked that strange because at some point you increased itemIndex too early. Avoid that.
@coder777

i use some accessor and mutator for the itemIndex already
so it's able to store right now.

but the program working fine on visual studio 2010.
and can i ask , why when i use netbean compile.
i repeat same step like i just displayOrderRecord()

after that i will have error stated that not working.
isn't some undefined behaviour make me get the errror like this?

can u spot it? or hard to spot?
did my
 
displayOrderRecord()

function there i do some undefined behaviour that i don't know?
because when i repeat to play this function.
the error will happen on netbean stated not working.
for this function only. other function works well
function there i do some undefined behaviour that i don't know?
Yes, it still uses itemIndex as a parameter which plain wrong. Remove it from that function as well. Only the member variable itemIndex from OrderList can be used.

So remove any itemIndex as a parameter and replace it with the according member variable of OrderList. Wherever that is the case

Otherwise you will have undefined behavior
i already delete and change to like what you request to do that new member with
ItemIndex
but same problem happen in netBean only..
for that function only.
if i call it 3-4 times.
the error will come out/
Well. Actually after placing debug output everywhere I found out that crashes caused by readInven() function, lines
inFile >> pricing; and inFile >> warranty; And I have no idea why. If I change it's types to int everything is working.

And if I run this with a debugger problem disappears too... :|
Last edited on
but my price is have a double value 1 what?

but i change the warranty from double to int
but the same problem happen also .

but the price double i still remain.
so how should i do a?
after i changed it i able to repeat the function for 5-6 times sometime.
then error occur again =.= .. i use debugge but i don't know how to figure it out ! . headache*
Pages: 12