-2

Possible Duplicate:
How to read integers from a file, line by line in C++

Please anyone can suggest me how to take input of integers until a newline in C++.

Suppose the input stream is

10 10 10 10 10 10 10 10 10 10, followed by a newline in C++.

Community
  • 1
  • 1
Luv
  • 5,381
  • 9
  • 48
  • 61
  • 4
    [so] isn't a code writing service. Have you tried anything at all? Are you writing a console application? A Windows form? Maybe start with `cin` and `cout` for some research material. – Cᴏʀʏ Aug 16 '12 at 18:01
  • 1
    this question is a mess, 2 languages and an undefined context where we are suppose to apply your requirements: you can ask about how to read from a console in C or how you read from a stream from a modem in C++ . – user827992 Aug 16 '12 at 18:04
  • @Cory I have tried much. If u want i can post that also – Luv Aug 16 '12 at 18:08
  • @tuğrulbüyükışık From keyboard – Luv Aug 16 '12 at 18:09
  • GetKeyState(key code); gives true/false if the key given is pressed or not. This way, you can check if user pressed "space" and "enter" keys – huseyin tugrul buyukisik Aug 16 '12 at 18:13

2 Answers2

5
std::string the_string;
std::getline(the_stream, the_string);
std::istringstream iss(the_string);
for (int n; iss >> n; )
{
    // do something with n
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
5

Likely duplicate of: How to read groups of integers from a file, line by line in C++

If you want to deal in a line per line basis:

int main()
{
   std::string line;
   std::vector< std::vector<int> > all_integers;
   while ( getline( std::cin, line ) ) {
      std::istringstream is( line );
      all_integers.push_back( 
            std::vector<int>( std::istream_iterator<int>(is),
                              std::istream_iterator<int>() ) );
   }
}
Community
  • 1
  • 1
Matt Razza
  • 3,524
  • 2
  • 25
  • 29