Force user input algorithm to stop at \n?

This is a discussion on Force user input algorithm to stop at \n? within the C++ Programming forums, part of the General Programming Boards category; I tried this little short little code: Code: #include <iostream> #include <algorithm> #include <vector> #pragma warning(push) #pragma warning(disable: 4512) #include ...

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033

    Force user input algorithm to stop at \n?

    I tried this little short little code:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #pragma warning(push)
    #pragma warning(disable: 4512)
    #include <boost/lambda/lambda.hpp>
    #pragma warning(pop)
    #include <iterator>
    
    int main()
    {
    	std::vector<int> MyIntegers;
    	std::cout << "Enter numbers: ";
    	std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(MyIntegers));
    	std::cout << "You entered the following numbers: ";
    	std::for_each(MyIntegers.begin(), MyIntegers.end(), std::cout << boost::lambda::_1 << " ");
    	std::cout << "\n";
    }
    While pretty awesome, it doesn't stop until I enter an invalid integer. It doesn't stop when it hits a newline.
    So instead of (preferably) having to enter: 1 2 3 4 5 6 7 8 9 10,
    I have to enter: 1 2 3 4 5 6 7 8 9 10 a
    I was just wondering if there is a way around it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Quote Originally Posted by Elysia
    While pretty awesome, it doesn't stop until I enter an invalid integer. It doesn't stop when it hits a newline.
    So instead of (preferably) having to enter: 1 2 3 4 5 6 7 8 9 10,
    I have to enter: 1 2 3 4 5 6 7 8 9 10 a
    You could trigger EOF too, I suppose.

    Quote Originally Posted by Elysia
    I was just wondering if there is a way around it?
    Use std::getline() and then use the string read to initialise a stringstream, then finally use that stringstream to replace your current dealings with std::cin. Also, you do not need to use a lambda here, methinks:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #pragma warning(push)
    #pragma warning(disable: 4512)
    #pragma warning(pop)
    #include <iterator>
    #include <sstream>
    
    int main()
    {
    	std::vector<int> MyIntegers;
    	std::cout << "Enter numbers: ";
    	std::string line;
    	std::getline(std::cin, line);
    	std::stringstream ss(line);
    	std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(MyIntegers));
    	std::cout << "You entered the following numbers: ";
    	std::copy(MyIntegers.begin(), MyIntegers.end(), std::ostream_iterator<int>(std::cout, " "));
    	std::cout << "\n";
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by laserlight View Post
    Use std::getline() and then use the string read to initialise a stringstream, then finally use that stringstream to replace your current dealings with std::cin. Also, you do not need to use a lambda here, methinks:
    Oh yes, didn't think of that.
    But this is a little verbose--to many lines. I don't know if there are any iterators that can treat the contents of a string as integers?
    Something like...

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    #include <sstream>
    
    int main()
    {
    	std::cout << "Enter numbers: ";
    	std::string line;
    	std::getline(std::cin, line);
    	std::vector<int> MyIntegers(std::some_iterator<int>(line), std::some_iterator<int>());
    	std::cout << "You entered the following numbers: ";
    	std::copy(MyIntegers.begin(), MyIntegers.end(), std::ostream_iterator<int>(std::cout, " "));
    	std::cout << "\n";
    }
    The idea is to abuse algorithms to produce as few lines as possible to read integers from the input and then output them back!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Quote Originally Posted by Elysia
    The idea is to abuse algorithms to produce as few lines as possible to read integers from the input and then output them back!
    This is roughly as long as yours, but what about:
    Code:
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <sstream>
    #include <string>
    
    int main()
    {
    	using namespace std;
    	cout << "Enter numbers: ";
    	string line;
    	getline(cin, line);
    	cout << "You entered the following numbers: ";
    	stringstream ss(line);
    	copy(istream_iterator<int>(ss), istream_iterator<int>(), ostream_iterator<int>(cout, " "));
    	cout << "\n";
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Though that is not quite as I'd like it. It defeats one purpose - to be able to store the input in a vector.
    I thought it would work, but apparently, the STL isn't flexible enough to provide a way to read input that way without extra steps. It's a shame.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to limit user input to a certain number of digits?
    By NewbGuy in forum C Programming
    Replies: 7
    Last Post: 05-08-2009, 10:57 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  4. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21