Reputation: 107
I 'm doing an exercise at http://www.spoj.com/problems/RETO7/, but I got stuck on reading the inputs, the count of input lines is unknown, each line has two number, there is also empty line, can anyone know the way to solve this problem. The input is pasted to the console.
The webpage I mentioned above provide us a machine to run code(for any problems that be also listed on the page). We can communicate with its input file through standard in standard out. For exam, for adding two integers exercise, the input contains lines follow the rules, the first line is the number of pairs of int to be calculate called n, the next n line each line has two integers. The out put is n lines each line contains the sum of pairs of integers above. To solve this problem, the code below is alright
#include <iostream>
using namespace std;
int main()
{
int n,i,a,b;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a>>b;
cout<<a+b<<endl;
}
//fflush(stdin);
//getchar();
}
But now the input rules is changed, there is no first line with the number of sum to be calculate, so I don't know how many call to "cin" to be made. And I'm looking for a solution of reading all the inputs , I just want it to only calculate for the input I paste to the console and nomore inputs after that. The input I paste for the code above is
7
1 2
3 4
5 6
7 8
9 10
23 34
56 67
and the result
3
7
11
15
19
57
123
ps:I don't realy know if the input may contain empty line,but in the example they provided it does Thanks for reading!
Upvotes: 0
Views: 329
Reputation: 107
I'm so stupid, finally I found a solution
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int a,b;
do
{
getline(cin,str);
if(str!="")
{
stringstream ss(str);
ss>>a>>b;
//do something
}
}
while(str!="");
fflush(stdin);
getchar();
}
Upvotes: 1