Help Begginer problem c++ segmentation "fail"

Hi, I try everything to fix this with pointers and etc... But I dont know if is me that doesn't know used them or not... Whatever, I keep getting this message in terminal of linux "segmentation fail", if you could help me I would be grateful, here is the code:

#include <iostream>
#include <string>
using namespace std;
int main()

{

int store_more;
int store_months[2][3];
int total_sales,month_min,month_max;

int l,m;



for (int l = 0; l < 2; l++){
for (int m = 0; m < 3; m++){

cout<<"Enter the sales of the month "<<m<<" the store "<<l<<": ";
cin>>store_months[l][m];
cout<<endl;



}
}


store_more[l] += store_months[l][m];

if (store_more[0] > store_more[1]){
cout<<"Store 0 sell more"<<endl;

}

else {
cout<<"Store 1 sell more"<<endl;


}

if (store_more[0] == store_more[1]){

cout<<"Both stores sell the same"<<endl;

}



return 0;
}
oo, an easy one :) This is good, seg faults are a pain sometimes.

store_more[l] += store_months[l][m];
what is the value of l at this point in the code? You just looped l to 0,1 and now its 2... out of bounds...

also store more does not even appear to be an array at all. That can't be good. I would ask how that even compiles, but I dread the answer.
Last edited on
This code shouldn't even compile. You are using store_more as if it was an array but you have declared it as an int. Also note that l and m are two different variables than l and m inside the nested loops so they are not initialized when you use them later in the code.
Also, please don't use l as a variable name. It's too hard to tell the difference between 1, l, and |, all of which have meaning in C++.
Topic archived. No new replies allowed.