When should my program stop reading input for problems without multiple test cases?
Some problems do not have multiple test cases, and are instead judged by testing your code multiple times on different input files. If the problem statement does not say there are multiple cases, do not assume there are.
Other problems mention multiple test cases, but don’t provide any limits on how many there could be. Your program should stop at the end of the input file. All programming languages have a way of testing this; for example, in Java, a BufferedReader’s readLine() method will return null. In C++, scanf returns the number of items successfully matched; EOF if it reaches the end of the file.
Read your programming language’s documentation to find out what it returns.
If you choose to test your program without the redirection method mentioned earlier, you can generate an EOF (end-of-file) character by pressing Ctrl-Z.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.00");
final double ch=0.50f ;
int WdAmnt =input.nextInt();
Double PrevBalnc=input.nextDouble();
boolean flag = false;
if(WdAmnt%5 ==0 && WdAmnt<=2000. && PrevBalnc <=2000.00 && WdAmnt>0 && PrevBalnc>=0.00)
flag=true;
else
flag=false;
if(WdAmnt!=0 && WdAmnt<=PrevBalnc && flag)
{
PrevBalnc=PrevBalnc-(WdAmnt+ch);
}
System.out.println( df.format(PrevBalnc));
}
}
whats wrong withis code?
In C++,
while(cin>>n)
To terminate the input, you can use Ctrl+Z
used in C/C++
while(scanf("%d",&n)!=EOF){
//your code is here
}
HAPPY CODING
Edit it and post it as a code. I don’t think anyone can understand this as it is. ( Take EDIT, Select your code, and press Ctrl + k if you’re on Windows, or look at the options on the EDIT and you will see code sample )
while(cin>>n)
{
//your code
}
this will do the job in c++
for java implement a try catch block
break on exception caught while reading
please anyone can add solution for python??
You can use try and except in python.
while True:
try:
your code
except EOFError:
break
Without try-catch
import sys
for t in sys.stdin:
#Do your thing
bro plz tell properly, how to read when test cases are not specified
Apart from try-catch, For Scanner class, will checking of something like scanner.hasNext() be safer? (not a scalable approach, but still)
For Java using BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(true){ String s = br.readLine(); if(s==null) break; System.out.println(s); }
It will work too but I don’t think its safer in any case although probably its more accepted approach while developing real products but for competitive its just waste of cpu cylces for one extra method call.
Also scanner is too slow and complete No for competitive programming
Thanks !
To take input till provided .i.e. when we don’t know the number of test cases beforehand, we can use
int n; //variable to take input in every test case
while(cin >> n){ // loop will run untill the input is provided
//intrenal code for each test case
}