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.
#include <iostream>
#include <vector>
usingnamespace 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);
}
elseif(ch=='\n') break; //should exit if the user hits ENTER??
//otherwise, the character is "eaten"
}
//do whatever
return 0;
}
//option 2
#include <iostream>
#include <sstream>
#include <vector>
usingnamespace 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;
}