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
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
usingnamespace 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
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
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).
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: