Please read the new rule regarding the restriction on the use of AI tools. ×

scopeInfinity's blog

By scopeInfinity, history, 8 years ago, In English

There are few problems in which we can't determine length of input. Program is meant to process all the input it is given and terminates after no input is left in stdin.

For Example

Problem : Print Sum in new line for every two integer in a line.

Sample Input —

5 6
10 15
3 4

Sample Output -

11
25
7

Similar Question on SPOJ

http://www.spoj.com/problems/NHAY/

What is a good way to handle this kind of Inputs??

  • Vote: I like it
  • -1
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +6 Vote: I do not like it

For two integer inputs, u can use while(cin>>a>>b) cout<<a+b; Or while(scanf("%d%d",&a,&b) == 2) cout<<a+b; Or while(scanf("%d%d",&a,&b) != EOF) cout<<a+b;

»
8 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it
while (cin >> n) { ... }

or you can check rdstate() and eofbit, take a look: http://www.cplusplus.com/reference/ios/ios/rdstate/