Programming with Passion

Make the best out of everything.

Monday, 6 April 2015

Take input when number of Inputs are not given in C++.

Take input when number of Inputs are not given in C++.

This is simple example of problems in which the of inputs are not given and also it can be use as a tricky programming.

#include <iostream.h>

int main() 
{
int t,c=0;
while(cin>>t)
c++;
cout<<c<<endl;
return 0;
 }
Here when input is given each time cin>>t will return '1' otherwise it will return '0' that makes while condition false. The number of inputs are store in variable 'c' as it increase by 1 each time while condition is true.

No comments:

Post a Comment