×

# problem with testing solution

 0 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 :) asked 19 Mar '12, 07:50 2★studd 1●2●2●4 accept rate: 0% 560●10●19●24

 1 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); answered 19 Mar '12, 19:59 4★shredder 130●3●4●10 accept rate: 28% worked like a charm . Thanks :) (01 May '12, 05:47) studd2★
 1 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 answered 19 Mar '12, 10:45 4★gultus ♦ 1.5k●1●13●25 accept rate: 51%
 1 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  answered 01 May '12, 12:40 16.9k●49●115●225 accept rate: 11%  0 Answer is hidden as author is suspended. Click here to view. answered 07 May '12, 06:23 0★urani (suspended) accept rate: 0%  toggle preview community wiki: Preview ### Follow this question By Email: Once you sign in you will be able to subscribe for any updates here By RSS: Answers Answers and Comments Markdown Basics • *italic* or _italic_ • **bold** or __bold__ • link:[text](http://url.com/ "title") • 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
×60

question asked: 19 Mar '12, 07:50

question was seen: 1,741 times

last updated: 24 Mar '16, 00:49