index 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
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
void addStore( Inventory *&inven , int &index ){

    string itemName = "";
    string modelName = "";
    double price = 0.00;
    int invenQuan = 0;
    int quantity = 0;
    char choice = 'a';
    double warranty = 0.00;
    bool matchItem = false;
    int matchIndex = 0;
    
    cout << "Enter item name : ";
    getline( cin , itemName );
    stringtoUpper( itemName );

    for( int i = 0 ; i < index ; i++ ){
        if( inven[i].getItemName() == itemName ){
            matchItem = true;
            matchIndex = i;
            break;
        }
    }
    if( matchItem == true ){
        cout << inven[matchIndex].getItemName() << endl
             << inven[matchIndex].getModel()    << endl
             << inven[matchIndex].getPrice()    << endl
             << inven[matchIndex].getQuantity() << endl
             << inven[matchIndex].getWarranty() << endl; 
        cout << "Do you want to restock ? [y/n] : ";
        cin  >> choice;
        do{
            switch( choice ){
                case 'y':
                case 'Y':
                    do{
                        cout << "Enter quantity to add : ";
                        cin  >> quantity;
                        while( cin.fail() ){
                            cin.clear();
                            cin.ignore(100,'\n');
                            cout << "Cannot contain Alphabet ! Please re-enter "<< endl;
                            cout << "Enter quantity to add : ";
                            cin  >> quantity;
                       }
                       if( quantity < 0){
                           cout << "Cannot put negative value !" << endl;
                       }
                   }while( quantity < 0 );
                   invenQuan = inven[matchIndex].getQuantity();
                   invenQuan += quantity;
                   inven[matchIndex].setQuantity(invenQuan);
                   break;
               case 'n':
               case 'N':
                   break;
               default:
                   cout << "Invalid input ! Please re-enter !" << endl;
                   break;
            }
        }while( choice != 'n' && choice != 'N' && choice != 'Y' && choice != 'y' );
        saveInventory( inven , index );
    }
    else{
		inven[index].setItemName( itemName );

        cout << "Enter model name : ";
        getline( cin , modelName );
        stringtoUpper( modelName );
        inven[index].setModel(modelName);

        do{
            cout << "Enter Price : ";
            cin  >> price;
            while( cin.fail() ){
                cin.clear();
                cin.ignore(100,'\n');
                cout << "Price cannot contain alphabet !" << endl;
                cout << "Enter Price : ";
                cin  >> price;
           }
            if( price <= 0 ){
                cout << "Invalid input ! Price cannot be negative !" << endl;
            }
        }while( price <= 0  );
        inven[index].setPrice(price);

        do{
            cout << "Enter Quantity : ";
            cin  >> quantity;
            while( cin.fail() ){
                cin.clear();
                cin.ignore(100,'\n');
                cout << "Quantity cannot contain alphabet !" << endl;
                cout << "Enter Quantity : ";
                cin  >> quantity;
           }
            if( quantity <= 0 ){
                cout << "Invalid input ! Quantity cannot be negative !" << endl;
            }
        }while( quantity <= 0 );
        inven[index].setQuantity(quantity);

        do{
            cout << "Enter warranty : ";
            cin  >> warranty;
            while( cin.fail() ){
                cin.clear();
                cin.ignore(100,'\n');
                cout << "Warranty cannot contain alphabet !" << endl;
                cout << "Enter warranty : ";
                cin  >> warranty;
           }
           if( warranty < 0 ){
                cout << "Invalid input ! warranty cannot be negative !" << endl;
           }
        }while( warranty < 0 );
        inven[index].setWarranty(warranty);
        index++;
        saveInventory( inven, index );
    }
}


can i ask why jump to line 65 . my program will occur problem and pop out error message?

here is my main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void callSystem(){
    ifstream inFile;
    Inventory *inven;
    OrderList *order;
    Tracking  *track;
    int index = 0;
    int orderIndex = 0;
    int itemIndex  = 0;
    char choice ='a';
    char invenChoice = 'a';
    char orderChoice = 'a';
    char trackChoice = 'a';
    
    readInven(inven,inFile,index);
    inven = new Inventory[index];   
    readOrder( order , orderIndex , itemIndex );
    recordStore(inven,index);
    readTracking( track , orderIndex , itemIndex );


i know i get the error because that
 
inven = new Inventory[index];


can someone teach me that if i call that function addStore
how should i initialize back my
 
inven = new Inventory[index]

can i recall it?
Last edited on
you convert itemName to upper (I assume that stringtoUpper() does that), but I guess that inven[i].getItemName() is not all uppercase when originally read [from file].

Are you sure that you want just uppercase for your itemName when inserting in your inventory?

further more: isn't index out of range?
You should look through function readInven(inven,inFile,index);
to determine what value of index is returned (if it is indeed returned through a parameter) to function callSystem.
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
void readInven( Inventory *&inven , ifstream &inFile , int &index ){
    inFile.open("inventory.txt");
    if( inFile.is_open() ){
        //cout << "Read data-ing..." << endl;

        inFile >> index;
        
        inven = new Inventory[index];

        string itemname  = "";
        string modelname = "";
        double pricing   = 0.00;
        double warranty  = 0.00;
        int quantity     = 0;

        for( int i = 0 ; i < index ; i++ ){
            inFile.ignore();
            getline( inFile , itemname );
            inven[i].setItemName(itemname);
            getline( inFile , modelname );
            inven[i].setModel(modelname);
            
            inFile >> pricing;
            inven[i].setPrice(pricing);
            inFile >> quantity;
            inven[i].setQuantity(quantity);  
            inFile >> warranty;
            inven[i].setWarranty(warranty);
        }
    }

    else
         cout << "Fail's open fail due empty " << endl;
}


you guys right.
my index out of range due to the
 
inven = new Inventory[index];


now the problem is when i addStore.
how should i redeclare the inven for new index again with still remain the same data inside

if i redeclare at addStore write
 
inven = new Inventory[++index];

all the previous data array will be overwrite and become empty,,
Last edited on
Topic archived. No new replies allowed.