Code doesn't output to console

This is a discussion on Code doesn't output to console within the C++ Programming forums, part of the General Programming Boards category; Hi. Can someone explain why this doesn't output to a console window using Dev-C++. system("PAUSE") doesn't work either. Thanks Code: ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Code doesn't output to console

    Hi. Can someone explain why this doesn't output to a console window using Dev-C++. system("PAUSE") doesn't work either. Thanks
    Code:
    #include<iostream>
    using namespace std;
    
    int fn(int x, int y)
    {
      if(x==y)
        return x;
      if(x<y)
      {
        int temp;
        temp=x;
        x=y;
        y=x;
      }
      return fn(x-y,y);
    }
    
    int main()
    {
      cout<<fn(27, 15);
      cin.get();
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    53
    What is your function actually supposed to do ? I think you meant x-1 and not x-y

  3. #3
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    And he probably meant to make y = temp instead of y = x.

    The only thing I can think of is you might not be using a console project. I never use DevC++ but prefer to stick to the command prompt.

  4. #4
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Thanks

    Actually, I believe that this function is an example of recursion. I really do mean fn(x-y,y). Maybe it should be:
    Code:
    ...
    temp=x;
    x=y;
    y=temp;
    ...

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by smitsky
    Actually, I believe that this function is an example of recursion. I really do mean fn(x-y,y). Maybe it should be:
    Code:
    ...
    temp=x;
    x=y;
    y=temp;
    ...
    That would be my guess. Did you try it? What did you intend this code to do? (Or, if you got it from someone/somewhere else, what do you think it's supposed to do?)

    Heres an example of what you could try to do to track it down yourself:

    Code:
    int fn(int x, int y)
    {
      int retval;
    
      if(x==y)
        return x;
      if(x<y)
      {
        int temp;
        temp=x;
        x=y;
        y=x;
      }
      cout << "before calling fn(" << x-y << ", " << y << ")" << endl;
      retval = fn(x-y,y);
      cout << " returning from fn(), with retval = " << retval << endl;
      return retval;
    }
    cout<< is a wonderful debugging tool.

    Regards,

    Dave

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    27x - 15y = 12x, 15y
    swap
    15x - 12y = 3x, 12y
    swap
    12x - 3y = 9x, 3y
    9x - 3y = 6x, 3y
    6x - 3y = 3x, 3y

    i'll be damned.....

    yea, change it to this
    cout << "See, your code will print if you printed it here";
    if(x<y)
    {
    int temp;
    temp=x;
    x=y;
    y=temp;
    }


    it's not that your code isn't printing...it's never ending and never reaches the cout....
    Last edited by misplaced; 10-03-2004 at 09:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output of code
    By lesodk in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2009, 09:56 AM
  2. Help editing console output
    By Longbow0705 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2009, 09:57 AM
  3. Text Justification for console output
    By greywolf0723 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2009, 01:29 AM
  4. redirect console output to a String?
    By TwistedMetal in forum C Programming
    Replies: 10
    Last Post: 02-26-2008, 02:54 PM
  5. Output to console window...
    By alvifarooq in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2004, 09:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21