Thread: blank output screen !!

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    blank output screen !!

    the following code does not print anything . why?

    Code:
    #include<iostream.h>
    #include<conio.h>
    void fn(int); 
    
    int main() {
    fn(10);
    getch();
    return(0);
    }
    
    void fn(int n)
          {
            static int i;
            for(i=0;i<=n;i++)
            fn(n-i);
            cout<<"anything\n" ;
          }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    well......first off ..... you keep calling fn() where n = 10....... n-i when i = 0 == n-0

    that should eventually segfault if i'm thinking correctly.....
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A recursive function needs some kind of test which if true returns from the function instead of calling the function again. The first time through your for loop n=10 and i=0, so n-i=10. Then, you call your function again with 10 as the argument. So, your program takes a break from executing the first function call, and exectutes the second function call. For your second function call, n=10 and i=0, so n-i=10, and you call your function again with 10 as the argument. So, your program takes a break from executing the 2nd function call and executes your 3rd function call...and so on and so on in an infinite cycle of function calls.
    Last edited by 7stud; 05-02-2005 at 08:34 AM.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    but i have declared i as static shd'nt it retain its value, or how to correct it.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    but everytime you call the function you destroy i's value by doing the i=0 in the for loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Render text
    By Livijn in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2007, 03:32 PM
  2. screen output question
    By f6ff in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2006, 08:43 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  5. dos output screen
    By niroopan in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2002, 09:03 AM