You are not logged in. Please login at to post your questions!


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] = {..};

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

studd's gravatar image

accept rate: 0%

edited 19 Mar '12, 19:23

tojochacko's gravatar image

2★tojochacko ♦

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:



answered 19 Mar '12, 19:59

shredder's gravatar image

accept rate: 28%

worked like a charm . Thanks :)

(01 May '12, 05:47) studd2★

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

Follow the simple instructions given at


answered 19 Mar '12, 10:45

gultus's gravatar image

4★gultus ♦
accept rate: 51%

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

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

edited 01 May '12, 22:48

Answer is hidden as author is suspended. Click here to view.

answered 07 May '12, 06:23

urani's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 19 Mar '12, 07:50

question was seen: 1,731 times

last updated: 24 Mar '16, 00:49