Reputation: 386
#include <iostream>
int main {
cin>>i;
cout<<i;
}
How do I change the cin
part so that it lets in any amount of inputs separated by a space?
Upvotes: 0
Views: 8710
Reputation: 386
I figured out what works best for unlimited inputs.
The best one is:
while (true){
cin >> i;
}
However, it does not take in spaces.
The one for taking in everything including spaces is getline, as mentioned by Dejay above. The last one, which is not as handy as while (true) but still works is the for (;;). It works pretty much like while (true) and does not recognize whitespace characters.
Ex.
for(;;){
cin >> i;
}
Upvotes: 0
Reputation: 1951
try this:
int num;
cout << "Enter numbers separated by a space" << endl;
do
{
cin >> num;
/* process num
or use array or std::vector to store num for later use
*/
}while (true);
This might answer your query.
Upvotes: 4
Reputation: 768
getline
might be what you're looking for. Example from CPlusPlus.com:
// istream::getline example
#include <iostream> // std::cin, std::cout
int main () {
char name[256], title[256];
std::cout << "Please, enter your name: ";
std::cin.getline (name,256);
std::cout << "Please, enter your favourite movie: ";
std::cin.getline (title,256);
std::cout << name << "'s favourite movie is " << title;
return 0;
}
Upvotes: 0