You can read a line of input with getline() and then parse the line using a stringstream.
The following program reads lines of input, adds all the numbers on that line and outputs the sum.
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string line;
while( getline( cin, line ) )
{
istringstream in( line );
int sum = 0, x;
while( in >> x ) sum += x;
printf( "%d\n", sum );
}
return 0;
}
If only I had as much free time as I did in college...
Thank you all. I think strtok() is easier and faster way to read.
I've written code which in my opinion is good (I got good answers on my computer) and I submitted it but I got Runtime Error (SIGSEGV).
I think the problem is the way I read data because I deleted all additional code and send only part that should read input. I got SIGSEGV again. Does anybody know what's wrong with it:
I tried every tricky input i could think of. Including k=0, non stopping elevators, taking elevators up- and downward, ... I'm using a modified dijkstra to add the time when changing elevators. And my program is correct for all example input and previously posted examples in this board.
I am pretty sure its not the input problem, unless of course input has some blank lines in it coz what I do is:
while( cin >> n >> t )
{
////
.
.
.
/////
for( i = 0; i < n; i++ )
{
cin >> speed;
}
// read end of the line character and get to the next line
getline(cin, s);
// read line by line
for( i = 0; i < n; i++ )
{
// read the whole line
getline(cin, s);
I've used Dijkstra's algorithm, but always getting WA.
Are there empty lines possibly filled with spaces in arbitrary positions in judge's input?
Please help
Each line in the input contains at least one integer. Make sure you handle the ground floor correctly. Does your code work when no elevator stops there, when the wrong elevator stops there, when all elevators stop there?
If only I had as much free time as I did in college...