strange testing behaviour

6 views
Skip to first unread message

David García Sánchez

unread,
Feb 18, 2012, 11:02:22 AM2/18/12
to proglang-c...@googlegroups.com
Hi!

I have problems with only one test, good11.cc, is the only one failing (and the already discussed core101.cc). This program is supposed to read numbers until it reaches a 0 and print their average. 
If I execute ./lab2 <tests_path>/good11.cc and write by hand in the input numbers, it behaves as supposed and correctly, but when I redirect the input from a file I receive an exception (java.util.NoSuchElement), when running the test suite, here is the result

../..//lab2 good/good11.cc failed: invalid output
For input file good/good11.cc:
---------------- begin good/good11.cc ------------------
//read numbers until 0 is read, and print their average

int main () 
{
  int sum = 0 ;
  int num = 0 ;
  int x ;
  while ((x = readInt()) != 0) {
    sum = sum + x ;
    num++ ;
  }
  printInt(sum/num) ;

}

----------------- end good/good11.cc -------------------
Given this input:
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1
0

It printed this to standard error:
java.util.NoSuchElementException

Expected output:
5

But as I said, if I execute the test alone by hand and put one by one these input numbers, I get the expected output. I've been debugging this for hours now. The source code for the interpretation of "readInt" is

if(p.id_=="readInt"){
Scanner sc = new Scanner(System.in);
return new Value.IntValue((Integer)sc.nextInt());
}

Is this a problem of java class Scanner when redirecting the standard input? it reads correctly the first number, but nothing more and so, since is expecting more input and the input is over, throws the NoSuchElement exception.

Thanks for the attention.

--
David García Sánchez
MSc Student, Computer Science
ARCO Research Group (Computer Architecture and Networks)
School of Computer Science, ESI.
UCLM, University of Castilla-La Mancha
Ciudad Real
Spain

David García Sánchez

unread,
Feb 18, 2012, 11:56:45 AM2/18/12
to proglang-c...@googlegroups.com
SOLVED!!

Ok, I solved the problem. It was that I declared a new Scanner object for the standard input each time I wanted to read something, that's why the first works, the second raises the exception. 

So, don't pay attention to this thread anymore. Sorry for the inconveniences.

Kind regards.
Reply all
Reply to author
Forward
0 new messages