thanks

i want to input integers till push enter,please help me.
I can definitely help you with that.

1
2
int i;
cin >> i;


As soon as you push enter, it'll take your integers.
Actually, this brings up a question I have. Is it possible to read a stream of integers separated by white space with:

1
2
3
4
vector<int> i;
vector<int>::iterator it;

vectori.push_back(cin);
Last edited on
i have never seen any code like this
one more thing i want to input a set of integers,but i don't know quantity of them,however i need it becuse i have to find maximum integer among them with for loop
Set a sentinel. This is basically a certain value that would not be used under normal circumstances that you can use as a reference to make a loop terminate. If you're expecting only positive integers, for instance, you could use '-1' and when the user enters -1, the loop will terminate.

Make a loop to keep taking integers and reading them into a vector, list or deque, and then make it terminate the loop and continue on to finding the largest among them whenever the sentinel is entered.
You might want to check this out:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
//...
vector< int > i;
copy( istream_iterator< int >( cin ),
      istream_iterator< int >(),
      back_inserter( i ) );


Some time long ago, one of the members here was all about stream iterators:
http://www.cplusplus.com/forum/general/7385/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main(){
    vector<int> i;
    //vector<int>::iterator it;

    cout << "Enter a string of integers separated by a space:" << endl;
    copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(i));
    //for(it = i.begin(); it != i.end(); it++)
    for(int j = 0; j < sizeof(i); j++)
        cout << i[j];
    return 0;
}


Compiled, but freezes.

I'm thinking it doesn't know how to stop taking in ints.
Last edited on
i am very thankful,but i don't understand these,sorry thanks.
Try one of these... I'm not near a compiler, so I can't test them for sure.

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

int main()
{
	char ch;
	int i;
	vector<int> ints;

	while(cin.get(ch)) { //pull out one character--doesn't skip whitespace (that's ok)

		//put a numeric character back and read the entire integer
		if(isdigit(ch)) {
			cin.unget();
			cin >> i;
			ints.push_back(i);
		}

		else if(ch=='\n') break; //should exit if the user hits ENTER??
		//otherwise, the character is "eaten"
	}

	//do whatever

	return 0;
}


Option #2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//option 2
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
	string s;
	int i;
	vector<int> ints;

	getline(cin, s, '\n'); //gets input until user hits enter
	stringstream ss(s); //copies entered input into a new stream

	while (ss >> i) ints.push_back(i); //when you run out of characters, this will terminate :)

	//do whatever

	return 0;
}
Last edited on
I didn't look at option 1, but option 2 returns successful results. I like sstream a lot better than char operations.
Last edited on
So, Qurbanli, I think this is what you were looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main(){
    string s;
    int i;
    vector<int> ints;
    vector<int>::iterator it;

    cout << "Enter a string of integers separated by a space:" << endl;
    getline(cin, s, '\n');
    stringstream ss(s);

    while(ss >> i) ints.push_back(i);

    for(it = ints.begin(); it != ints.end(); it++)
        cout << *it << endl;
    return 0;
}
Compiled, but freezes.

I'm thinking it doesn't know how to stop taking in ints.


Streams are terminated with EOF. Try ctrl-d.
Topic archived. No new replies allowed.