Segmentation Error, Etc...

Can someone explain why I get a segmentation error in this code? I am a newbie, student, and just trying to get my work done.

Also, I need to pass the values (or references)for the region[] and sales[] to be compared with each other. It seems that it would be easier to do it in main(), but I am supposed to used a function to do it. Suggestions?

Here's the code I am having trouble with:

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
#include <iostream>
using namespace std;

// Defined function prototypes
double getSales(string); // Funtion to get quarter sales
// void findHighest(double); // Function to determine highest sales

int main()
{
    // Declare local variables
    int index, counter;
    string region[index];
    double sales[index];

    cout << "\nHow many sales regions do we have this quarter? ";
    cin >> index;

    for (counter=1; counter <= index; counter++)
    {
        cout << "\n\nWhat is the name of region " << counter << ": ";
        cin >> region[counter];
        sales[counter] = getSales(region[counter]);
    }

    for (counter = 1; counter <= index; counter++)
    {
        cout << "\nSales for " << region[counter] << " is $" << sales[counter] <<  endl;
    }

    return 0;
}

// Funtion to get quarterly sales
double getSales(string value1)
{
    // Local variable
    double value2;

    cout << "\n\nEnter the sales total for the " << value1 << " region: ";
    cin >> value2;

    return value2;
}
Last edited on
Hey. Please edit your code so it's between code tags - http://www.cplusplus.com/articles/jEywvCM9/


1
2
3
int index, counter;
string region[index];
double sales[index];


You can't create 2 arrays with the size of index, which is a garbage value since you haven't assigned any number to int index;

Even if you did you still couldn't do it this way. Because index wouldn't be a constant value, which you need for an array.

1
2
int index = 5;
int arr[index]; // this doesn't work. 


int arr[5]; // this works.
You're trying to use index as an allocation size before it is initialized.
1
2
3
int index, counter;
string region[index];  // What do you think the value of index is here?
double sales[index];   // Hint:  It's garbage. 


Also, using a non-const variable as an array dimension is not standard C++.

When you allocate an array, the elements are 0 to n-1. Not 1 to n.
1
2
3
4
5
for (counter=1; counter <= index; counter++)
{  cout << "\n\nWhat is the name of region " << counter << ": ";
    cin >> region[counter];
    sales[counter] = getSales(region[counter]);
}

If index is 20, you're going to be making an out of bounds reference to region[20], since the elements are 0-19.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
I understand the code tags now. :)

Thanks...

I can define the index value, I just was thinking (silly me) that it would better to make it "generic." I guess not.

Philip
Last edited on
So, I changed the index to a constant value of 4. I can now run the program and enter values for three passes to the function call, one the 4th pass, it crashes with the same segmentation error 139 before going to the function. I bolded the code when it crashes.

I do appreciate you folks humoring me, since I know basic issues are not generally tolerated.

Code below:

1
2
3
4
5
6
7
8
9
 for (counter = 1; counter <= 4; counter++)
    {
        cout << "\n\nWhat is the name of region " << counter << ": ";
        cin >> region[counter];
        sales[counter] = getSales(region[counter]);

        cout << "\ncounter = " << counter << endl; // testing
        cout << "\nsales[counter] = " << sales[counter] << endl; // testing
    }
You're still making an out of bounds reference.

You missed my earlier point about how elements are accessed.
Array elements are 0 based, not 1 based.

If you declared:
 
  string region[4];

The elements are region[0] to region[3]. region[4] is an out of bounds reference.
Note that your loop goes from 1 to 4.
You want your loop to go from 0 to 3.
The usual idiom for this is:
 
for (counter = 0; counter < 4; counter++)  // Note the < 4, not <= 


You will want to change your prompt at line 3 to:
 
cout << "\n\nWhat is the name of region " << counter+1 << ": ";


Last edited on
Topic archived. No new replies allowed.