-
December 19th, 2011, 02:03 PM
#1
Multiple variables with cin
Hi
Is it possible to read non-fixed amount of variables with one cin and push'em to a vector?
Thanks in advance
-
December 19th, 2011, 02:24 PM
#2
Re: Multiple variables with cin
You will need to clarify. Obviously it is not possible to write a cin statement in the code reading a variable number of inputs in that statement, unless your program is generating, compiling, and invoking other code. (Unlikely.)
However, from a programming perspective there's no particular reason to impose this limitation. What is your actual goal?
-
December 19th, 2011, 03:55 PM
#3
Re: Multiple variables with cin
As Lindley said, we need more details of your problem + what's your level of expertise in C++.
You could use std::copy with std::istream_iterator and std::back_inserter, something like:
Code:
vector<int> vec;
copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(vec));
This reads a variable number of integers from cin and pushes them in the vector vec.
-
January 15th, 2012, 10:09 AM
#4
Re: Multiple variables with cin
Originally Posted by Marc G
As Lindley said, we need more details of your problem + what's your level of expertise in C++.
You could use std::copy with std::istream_iterator and std::back_inserter, something like:
Code:
vector<int> vec;
copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(vec));
This reads a variable number of integers from cin and pushes them in the vector vec.
This way it's never ending. I cannot get to the next commands
-
January 15th, 2012, 10:40 AM
#5
Re: Multiple variables with cin
if you don't know what indicates the end of the input, your program won't either now won't it?
-
January 16th, 2012, 03:02 AM
#6
Re: Multiple variables with cin
Originally Posted by marmistrz
This way it's never ending. I cannot get to the next commands
A stream on Windows is ended with Control+Z.
-
January 16th, 2012, 04:09 AM
#7
Re: Multiple variables with cin
Originally Posted by marmistrz
This way it's never ending. I cannot get to the next commands
note that you'll need to std::cin.clear() ( and std::cin.ignore, if ctrl-z has not been used to end the stream ) to continue extraction ...
-
January 16th, 2012, 10:33 AM
#8
Re: Multiple variables with cin
I know what ends. The end of line.
There's n variables (the amount I'm given in runtime) anit's like that:
a b c d (n variables)
for n=3
Last edited by marmistrz; January 16th, 2012 at 10:39 AM.
-
January 16th, 2012, 11:40 AM
#9
Re: Multiple variables with cin
Originally Posted by marmistrz
I know what ends. The end of line.
There's n variables (the amount I'm given in runtime) anit's like that:
a b c d (n variables)
for n=3
In that case, you want to read in an entire line, and then process that line.
There are several ways to do this.
- Process the line with regexes. This is usually efficient, but requires more knowledge of regexes.
- Feed the line into a string stream, and then read from that stream. There are two extra advantages with this approach:
---if the input is bad, it does not put your cin in a fail state.
---You are still processing streams, so you can reuse code, apply divide and conquer, or even recurse.
Here is an example using stringstream: My goal is to read an entire line, and then read each character 1 by 1 (NOTE: cin replaced by cin2 for slf containing example):
Code:
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
std::stringstream cin2(
"abc\n"
"def\n"
"gh"
);
int main()
{
std::string line;
while(std::getline(cin2, line))
{
std::cout << "Line: ";
std::istringstream issline(line);
std::istream_iterator<char> it_first(issline);
std::istream_iterator<char> it_last;
std::ostream_iterator<char> it_out(std::cout, "-");
std::copy(it_first, it_last, it_out);
std::cout << std::endl;
}
}
Code:
Line: a-b-c-
Line: d-e-f-
Line: g-h-
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 16th, 2012, 12:16 PM
#10
Re: Multiple variables with cin
And if I assume the input's always correct?
I'm making a prog to make my life easier
I'd be happy with an array of int
-
January 17th, 2012, 03:10 AM
#11
Re: Multiple variables with cin
Originally Posted by marmistrz
And if I assume the input's always correct?
I'm making a prog to make my life easier
I'd be happy with an array of int
If you want an easy life, then use a vector, not an array.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
January 17th, 2012, 01:37 PM
#12
Re: Multiple variables with cin
Yeah. But how then to read n variables in a row divided by spaces?
-
January 17th, 2012, 01:57 PM
#13
Re: Multiple variables with cin
Originally Posted by marmistrz
Yeah. But how then to read n variables in a row divided by spaces?
Well, the >> operator stops at whitespace, so simply doing
cin >> var[i];
in a loop would work. The only difficulty there is knowing when to stop, since a newline will be treated identically to any other whitespace with this method.
I would suggest first reading a single line into a std::string using getline(), and then parsing it as above using an istringstream.
-
January 18th, 2012, 11:58 AM
#14
Re: Multiple variables with cin
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|