×

# How should I take input in problems like these?

 0 Can anyone please help me in understanding how to take input in problems like these which do not specify the number of test cases? asked 20 Jul '17, 18:44 144●8 accept rate: 8%

 2 int n; while(cin>>n) { //your logic } answered 20 Jul '17, 19:02 1.7k●2●10 accept rate: 14%
 1 You can simply use: while(scanf("%d", &n) == 1) { //Do Something } answered 20 Jul '17, 19:04 2.5k●4●21 accept rate: 17%
 0 You can use a custom input method and check for EOF that is End Of File. int input() { char c; int flag=0; c=getchar(); if(c==EOF) return -1; while(c!='\0')//stop input when null character is encountered meaning a number has ended { //dow what you want //save the data or operate on data c=getchar(); } //here return the data acquired return 0;  } This will take input character by character . It is upto you how to use those characters . You can transform it into a number or store it as a string and much much more . In the loop in main you can check for if the input () function return -1 to stop executing the program. answered 20 Jul '17, 19:08 986●1●9 accept rate: 12%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×2,738
×170
×7

question asked: 20 Jul '17, 18:44

question was seen: 526 times

last updated: 20 Jul '17, 19:08