this is what i'm doing..

cout << "Value:" << endl;
cin>>it; //it is int
cout<<1;
for (pos = v->begin(); pos != v->end(); pos++) { // pos is an iterator and v is a vector<int>
	if (*pos < it) {
		v2->push_back(*pos);
		cout << *pos << endl;
	}
}
cout<<2;

This is what i get

Value:
2

then freezes.
I tried "std::cin.get();" but it gets worse: it freezes before i can insert.

How is 'it' defined? Please provide compilable code that produces the problem so we can take a look.

Dave

Full source:

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

int menu (){
	int i;
	cout << "1. Create a new vector."<<endl;
	cout << "2. Insert elements in the vector."<<endl;
	cout << "3. Show the vector."<<endl;
	cout << "4. Get all smaller than..."<<endl;
	cout << "5. Concatenate the first two."<<endl;
	cout << "0. Exit."<<endl;
	cin>>i;
	return i ;

}

int main() {
	int menuSelection;
	int nr_of_elements;
	int myints[] = {1,2,3,4,5,6,7,8,9,0};
	vector<int> *v = NULL	;
	vector<int> *v2 = NULL;
	vector<int> *v3 = NULL;
	vector<int>::iterator pos;
	int it;

	do{
		menuSelection = menu();
		switch(menuSelection){
		case 0: break;
		case 1:
			if(v != NULL){
				delete v;
				v = NULL;
			}
			v = new vector<int>();

			break;
		case 2:
			cout << "How many elements?" << endl;
			cin>>nr_of_elements;
			int input;
			for(int i =0 ;i<nr_of_elements;i++){
				cin>> input;
				v->push_back(input);
			}
			break;
		case 3:
			cout<< "Which one?"<<endl;
			it;
			cin>>it;
			if(it==1){
				for(pos = v->begin();pos !=v->end(); pos++){
					cout<< *pos << ' ';
				}
			}else if(it==2){
				for (pos = v2->begin(); pos != v2->end(); pos++) {
					cout << *pos << ' ';
				}
			}else if(it==3){
				for (pos = v3->begin(); pos != v3->end(); pos++) {
					cout << *pos << ' ';
				}
			}
				cout<<endl;
				break;
		case 4:
			cout << "Value:" << endl;
			it = cin.get();
//			it = atoi(getch());
			cout<<1;
			for (pos = v->begin(); pos != v->end(); pos++) {
				if (*pos < it) {
					v2->push_back(*pos);
					cout << *pos << endl;
				}
			}
			cout<<2;
			break;

		case 5:

			break;
		}
	}while(menuSelection != 0);
	return 0;
}

You have to select "1" before you can select "2" and start adding elements - else the vector is not defined! I'd suggest getting rid of all of the cod that is not necessary, setting the inputs you can in the code, and posting the shortest compilable example of what is going wrong.

Dave

1. Create a new vector.
2. Insert elements in the vector.
3. Show the vector.
4. Get all smaller than...
5. Concatenate the first two.
0. Exit.
1  //here i select option 1
1. Create a new vector.
2. Insert elements in the vector.
3. Show the vector.
4. Get all smaller than...
5. Concatenate the first two.
0. Exit.
2 //here i select option  2
How many elements?
3 // i will insert 3 elements
1 //1st element
2 //2nd
23 // 3d element
1. Create a new vector.
2. Insert elements in the vector.
3. Show the vector.
4. Get all smaller than...
5. Concatenate the first two.
0. Exit.
3 // here i select option 3
Which one?
1 
1 2 23 // elements of the vector (all and good)
1. Create a new vector.
2. Insert elements in the vector.
3. Show the vector.
4. Get all smaller than...
5. Concatenate the first two.
0. Exit.
4  //here i select option 4
Value:

It stops here with cin.get();
With "cin>>it;" it allows me to insert "1" for Value, after which the executions goes further! If i insert higher than 1: it freezes!
What's there to minimize. It is initiated, it has values, the "cin" thing doesn't work... it stops at that line.

Using Linux, Eclipse, GCC compiler.

Hi Alex_ :-)

In "Select all smaller than...":

*) Why don't you just use 'cin >> it;' to read the number, like you do everywhere else.

*) When you find a number smaller than 'it', you push it to the back of the vector v2. But v2 is just a null-pointer.

Fix those two problems and it should work :)

commented: 10x +2

Hey, that did it. No, not cin>>it, this i tried before too. The problem was that v2 was not allocated. I don't know what this has to do with stopping at cin>>it,but it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.