Vector Segmentation Fault

Hi, I have a error with one of my programs. I'm supposed to get rid of negative numbers when there are numbers that are randomly generated.
Here is the middle part of the code.

{

int vectorLength = 10;

vector<int> bothSigns(vectorLength);
cout << " Input vector: ";
for (int i = 0; i < vectorLength; i = i + 1)
{ bothSigns[i] = rand()%201 - 100;
cout << "\t" << bothSigns[i];
}
cout << "\n";

vector<int> noNegs;

////////////////////////////////////////////
for(int i = 0; i < vectorLength; i = i + 1)
{
if(bothSigns[i] > 0)
{
bothSigns.push_back(noNegs[i]);
}


}

The part where i'm supposed to start is after the /////'s. However, whenever I input a number for the random numbers(not put in part of code), i keep getting a segmentation error.

Edit: Never mind I got it.
Last edited on
Frankli0 wrote:
However, whenever I input a number for the random numbers(not put in part of code)
I don't understand what you mean by this.
1
2
3
4
5
6
7
for(int i = 0; i < vectorLength; i = i + 1)
	{
		if(bothSigns[i] > 0)
		{
			noNegs.push_back(bothSigns[i]);
		}
	}
Ah, I see, you got your vectors mixed up in this statement:

bothSigns.push_back(noNegs[i]);

It should be as naraku shows.
Topic archived. No new replies allowed.