Console crashing with random symbols

Hello, I am very new with vectors so I decided to make a small store simulator very far from completion. The way it works is it has a class called Store and two functions one createStore() which allows you to create the store, and openStore(vector<string>, vector<float>) which opens the store you create from the createStore() function. I have narrowed the functions to say that I believe the error is coming from the openStore function. I will paste all of the code then on the bottom the openStore function


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
#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;

    int money = 0;
    int lengthStore = 0;
    int rating;
    int totalCustomers;
    int i = 0;
    float insertPriceItemStore;
    string storeName;
    string insertItemStore;
    vector<string> storeList;
    vector<float> storeList_Cost;

    class Store{
        public:

            void createStore(){
                while(!(i == 1)){

                    cout << "\nEnter an item to add to your store, enter -1 to finish" << endl;
                    cout << "You can type -1 after you have added the first two item to the store.\n" << endl;
                    cin >> insertItemStore;

                    if(insertItemStore == "-1" && lengthStore >= 2){
                        cout << "\nYou have entered: " << lengthStore << " items into your shop!" << endl;
                        storeList.erase(storeList.begin()+lengthStore-1);
                        i = 1;

                    } else {

                    cout << "\nNow enter the price for the item you have just added" << endl;
                    cin >> insertPriceItemStore;

                    storeList_Cost.push_back(insertPriceItemStore);
                    storeList.push_back(insertItemStore);
                    lengthStore++;

                    }
                }
                cout << "You have finished creating the store!" << endl;

            }

            void openStore(vector<string>, vector<float>){

                        cout << "----Welcome to: " << storeName << " ----" << endl;

                    for(int x = 1; x <= lengthStore + 1; x++){
                        cout << "Item " << x << ": " << storeList[x] << " : " << storeList_Cost[x] <<  endl;

                    }
            }
    };

int main(){

    Store storeObject;

    storeObject.createStore();
    storeObject.openStore(storeList, storeList_Cost);

}

/////////////////////////////////////////////////////////////////////////////////////
The openStore(vector<string>, vector<float>) function
1
2
3
4
5
6
7
8
9
10
            void openStore(vector<string>, vector<float>){

                        cout << "----Welcome to: " << storeName << " ----" << endl;

                    for(int x = 1; x <= lengthStore + 1; x++){
                        cout << "Item " << x << ": " << storeList[x] << " : " << storeList_Cost[x] <<  endl;

                    }
            }
    };


This is what the random symbols look like (well there not really random, or symbols)

TEasf # #πo ╕►\ X♫\     - µo ╕►\ X♫\ 0╢Wu ∟ ∟
Γo ╕►\ h♫\
0╢Wu 0╢Wu 0╢Wu 0╢Wu 0╢Wu
☺ ☺ (bbu @,Wu└.Wu@0Wu╨cbun♀±1┬☻
0╢Wu
0╢Wu 0╢Wu 0╢Wu 0╢Wu ☺ ☺
♀≡S∙o ≡'\ á♫\
\Common7\Tools\ WATCOM=♦ ♦ëo x♫\ └ \ =C:\Windows 7♀±hÜo☻ Σ♦
►►►►►
►►►►►►►►►►►►►►►►►►►►► ► ► ►
► ►►►►►►►►►►►►►►►►►►►►►►► ►►►►►►►

abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ â
Ü £ ₧ è î Ä  ¬ ╡ ║ αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡
±♫áo ¶ê↨\ º↨\ ╬↨\ ♣↑\ A↑\ r↑\ å↑\ ¬↑\ ╛↑\ ±↑\ ■↑\ ¶↓\ >↓\ S↓\ j↓\ ë↓\ ù↓\ æ∟\ ╙∟
\ Σ∟\ ±∟\ ♀↔\ )↔\ q↔\ â↔\ ¢↔\ ╢↔\ ┌↔\ ♥▲\ !▲\ b▲\ y▲\ ì▲\ £▲\ ▓▲\ ┘▲\  ▲\ ◄▼\ 2▼
\ D▼\ _▼\ ▒▼\ ┬▼\


I have deleted some of the random symbols that said a lot about my computer
Last edited on
You access your vectors with invalid indices. Index starts with 0 and goes to size() - 1.
 
for(int x = 0; x < lengthStore; x++)
Thanks Thomas1965, I think fixed the indices problem but im still getting a crashing console

The for loop

1
2
3
4
 for(int x = 1; x <= lengthStore; x++){
                        cout << "\nItem " << x << ": " << storeList[x];
                        cout<< " : " << storeList_Cost[x] << "$";
                    }


This seems to be where the problem is, the loop should repeat the code based on how many items you input into the store vector, but it run three times max
Last edited on
I'd recommend removing all the global variables:
8
9
10
11
12
13
14
15
16
17
    int money = 0;
    int lengthStore = 0;
    int rating;
    int totalCustomers;
    int i = 0;
    float insertPriceItemStore;
    string storeName;
    string insertItemStore;
    vector<string> storeList;
    vector<float> storeList_Cost;


Possibly the two vectors may be justified and could remain, the rest just makes the code seem fragile and easily broken. Declare the variables as close as possible to the place where they are used. Having a class which is not self-contained but is dependent on external variables floating about somewhere doesn't make for a clean or resilient design.

In particular the variable lengthStore I think should not exist at all, vectors can take care of remembering their own size quite nicely without having to duplicate that (and possibly get out of step).

Just curious, what is the purpose of this?
 
        storeList.erase(storeList.begin()+lengthStore-1);

Last edited on
@Chervil

storeList.erase(storeList.begin()+lengthStore-1); That is to delete the -1 inside of the storeVector so in the shop there is no item named -1 inside the store. Thank you for the reply Chervil
Thanks, yes, I thought about it a little bit and figured out that was probably the reason. I hope you don't think I was being unhelpful I actually looked at the program and re-designed it somewhat, thought there still remain a few issues. I don't think this is exactly what you are looking for, but you might find it useful to consider:

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
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct storeItem {
    std::string name;
    float cost;
};


class Store {

private:
    vector <storeItem> storeList;
    string storeName;        
   
public:
    
    Store() { }
    Store(string name) : storeName(name) {  }

    void createStore() {
        
        bool done = false;
         
        while(!done) {

            cout << "\nEnter an item to add to your store, enter -1 to finish" << endl;
            cout << "You can type -1 after you have added the first two item to the store.\n" << endl;
            
            storeItem item;

            cin >> item.name;

            if (item.name == "-1" && storeList.size() > 1)
            {
                cout << "\nYou have entered: " << storeList.size() << " items into your shop!" << endl;
//              storeList.erase(storeList.begin() + lengthStore-1);  // not sure this is needed
                done = true;
            } 
            else 
            {
                cout << "\nNow enter the price for the item you have just added" << endl;
                cin >>  item.cost;
                storeList.push_back(item);
            }
        }
        
        cout << "You have finished creating the store!" << endl;
    }

    void openStore() {

        cout << "----Welcome to: " << storeName << " ----" << endl;

        for (unsigned int x = 0; x <  storeList.size(); x++)
        {
                cout << "Item " << x+1 << ": " << storeList[x].name << " : " << storeList[x].cost <<  endl;

        }
    }
};

int main()
{
    Store storeObject("Joes Supplies");
    storeObject.createStore();
    storeObject.openStore();
}


Last edited on
Thanks chervil for the help the program run's perfectly thank you very much
Topic archived. No new replies allowed.