Page 1 of 1
how I can read until eoln in c++?
Posted: Fri Aug 25, 2006 10:39 am
by Giorgi
please help to read integer numbers until eoln.
in pascal a do it so:
Code: Select all
var k:integer;
begin
while not eoln do
begin
read(k);
.
.
.
end;
end.
Posted: Fri Aug 25, 2006 12:03 pm
by mf
You can do it with this code:
Code: Select all
int k; char c;
while (scanf("%d%c", &k, &c) == 2 && c != '\n') { ... }
(Of course, this assumes that there are no trailing spaces at the end of line.)
Posted: Fri Aug 25, 2006 12:52 pm
by Giorgi
mf wrote:You can do it with this code:
Code: Select all
int k; char c;
while (scanf("%d%c", &k, &c) == 2 && c != '\n') { ... }
(Of course, this assumes that there are no trailing spaces at the end of line.)
thank you
Posted: Sat Aug 26, 2006 10:16 pm
by Martin Macko
mf wrote:(Of course, this assumes that there are no trailing spaces at the end of line.)
You can skip any trailing spaces by
"%*[ ]".
........scanf("%d%*[ ]", &a);
........scanf("%c", &c);
Posted: Mon Aug 28, 2006 10:08 am
by Zaspire
You can simple get line and then use std::istringstream
Re: how I can read until eoln in c++?
Posted: Wed Oct 29, 2014 6:37 pm
by Zyaad Jaunnoo
Code: Select all
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string line;
getline(cin, line);
stringstream stream(line);
int k;
while (stream >> k)
{
cout << k << endl;
}
return 0;
}