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?
#include <iostream>
usingnamespace 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;
}
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.
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 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 << ": ";