CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2011
    Posts
    21

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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?

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Dec 2011
    Posts
    21

    Re: Multiple variables with cin

    Quote Originally Posted by Marc G View Post
    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

  5. #5
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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?

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Multiple variables with cin

    Quote Originally Posted by marmistrz View Post
    This way it's never ending. I cannot get to the next commands
    A stream on Windows is ended with Control+Z.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Multiple variables with cin

    Quote Originally Posted by marmistrz View Post
    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 ...

  8. #8
    Join Date
    Dec 2011
    Posts
    21

    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
    Code:
    a b c
    Last edited by marmistrz; January 16th, 2012 at 10:39 AM.

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Multiple variables with cin

    Quote Originally Posted by marmistrz View Post
    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
    Code:
    a b c
    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.

  10. #10
    Join Date
    Dec 2011
    Posts
    21

    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

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Multiple variables with cin

    Quote Originally Posted by marmistrz View Post
    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.

  12. #12
    Join Date
    Dec 2011
    Posts
    21

    Re: Multiple variables with cin

    Yeah. But how then to read n variables in a row divided by spaces?

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Multiple variables with cin

    Quote Originally Posted by marmistrz View Post
    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.

  14. #14
    Join Date
    Dec 2011
    Posts
    21

    Re: Multiple variables with cin

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured