Run time error

May 28, 2011 at 6:39pm
I started rewriting one of my old programs with what I know now. The thing is Ive been getting this runtime error in my checkInventory() function, I have no clue what is wrong with it. It worked before it just stopped working I tried rewriting that section just isn't doing anything. Heres the code.
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

class InventoryX {
    public:
    int size;
    string Items[];
    int itemQuantity[];
};
void addInventory()
{
    InventoryX iSize;
    cout << "How many items do you want to keep inventory for? ";
    cin >> iSize.size;
    string Items[iSize.size];
    int itemQuantity[iSize.size];
    for (int i = 0; i < iSize.size; i++){
        cout << "Item name " << i << ": ";
        cin >> Items[i];
    }
    for (int i = 0; i < iSize.size; i++) {
        cout << "Quantity for " <<  Items[i] << " ";
        cin >> itemQuantity[i];
    }
    system("cls");
    cout << "\nUpdating Inventory....." << endl;
    for (int i = 0; i < iSize.size; i++){
        cout << Items[i] << ": " << itemQuantity[i] << endl;
    }
    for(int i = 0; i < iSize.size; i++){
        ofstream Inventory("inventory.txt", ios::app | ios::out | ios::in);
        Inventory << Items[i] << ": " << itemQuantity[i] << endl;
    }
}

void checkInventory()
{
    InventoryX itemsX;
    InventoryX iSize;
    for (int i = 0; i < iSize.size; i++){
        system("cls");
        cout << "Current Inventory\n" << endl;
        ifstream Inventory("inventory.txt");
        if(Inventory.is_open()){
            while(true){
                getline(Inventory, itemsX.Items[i]);
                if(!Inventory.good()) break;
                cout << itemsX.Items[i] << endl;
            }
            Inventory.close();
            break;
        }else{
            cout << "No inventory added. " << endl;
            break;
        }
    }
}

void editInventory()
{


}


int main()
{
    int choice, choice2;
    do {
    system("cls");
    cout << "Inventory \n" << endl;
    cout << "1. Add Inventory " << endl;
    cout << "2. Check Inventory " << endl;
    cout << "3. Edit Inventory " << endl;
    cin >> choice;

    switch ( choice )
    {
        case 1: addInventory(); break;
        case 2: checkInventory(); break;
        case 3: editInventory(); break;
    }

    cout << "\n1. Main Menu " << endl;
    cout << "2. Exit " << endl;
    cin >> choice2;
    }while (choice2 == 1);
    if (choice2 == 2)
        std::exit(EXIT_FAILURE);

    return EXIT_SUCCESS;
}
May 28, 2011 at 7:31pm
closed account (S6k9GNh0)
What are you using to compile this exactly? I just ran it through three different compilers, all of which slapped me in the face for trying to compile this example.

http://codepad.org/xb1f3Vjs

It only shows one error because of the compiler settings but there's probably 20 or so after that.
May 28, 2011 at 7:34pm
closed account (zb0S216C)
checkInventory()

In checkInventory( ), the members of iSize weren't initialized. You then used the member size of iSize in the for loop.

Also, you say that: The thing is Ive been getting this runtime error. Can describe what happens?
Last edited on May 28, 2011 at 7:37pm
May 28, 2011 at 9:23pm
code blocks is my IDE. When it comes to the main menu I press 2 for option 2 which is check Inventory and I get a runtime error then it exits.
May 28, 2011 at 10:23pm
closed account (zb0S216C)
The member Items of instance itemsX, has an undefined size. On line 48, you extract a line from the file and store it into Items which is an array of an undefined size. You really should define a size for Items beforehand.
Topic archived. No new replies allowed.