problem with testing solution

hi all ,

This is my first time here . I have surfaced out a problem I am facing and wanted to ask you guys for a better solution.

Problem:
After implementing the module of algo , I test the module from main by giving values to it not from file or stdinput but by declaring variables and giving it values. For ex

#define N 10
int sum(int *values){
....
}

int main(){
int values[N] = {..};
sum(values);
}

In the above program the problem generally states to take
N value from stdin and
array values from stdin.

I believe it is not efficient to test the program each time by giving required values from stdin…

The problem I am facing with my method is I that have to write the content of main method two times one for testing and one while submission. Some times after writing the submission main method If i get any errors then it is a pain to comment out this part and switch back to testing code. Again while switching I have to change lot of variable values to perform testing.

I will be pleased if my problem is solved .
I appreciate Codechef for the new improved forum :slight_smile:

You don’t need to struggle so much. Just write a single program reading from stdin.

Follow the simple instructions given at http://www.codechef.com/wiki/faq#How_should_I_test_my_program

1 Like

well the easiest way for checking would be to have a file say “input.txt” with the test cases in it,
then you could use
freopen() to redirect input to stdin from the file, like this for example:

freopen(“input.txt”,“r”,stdin);

1 Like

worked like a charm . Thanks :slight_smile:

Also you can read from stdin and redirect input to your program (I prefer this way). When your code is compiled (and a.exe is executable) just invoke from command line:

> a.exe < input.txt

I prefer this way because when there is no filename hardcoded in your code so you can use new file if needed.

It works same way on linux

$ ./a < input.txt

If you need to store output (for example in Google Code Jam output is submitted), you add redirection to file

> a.exe < input.txt > output.txt

Good answer,
Thanks a lot!