Thread: Informational question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Informational question...

    Okay, I have had problems with this since I started programming, and have only found one solution so far so I am going to ask for any other ways. In my programs I have a tendency to use recursion which I have learned is not good for large programs, and it seems to create problems exiting a program from within itself. After using recursion the program fails to exit like I want it just goes back to the calling function, thus making impossible for me to exit the program at will ( or at least I think ). My way of fixing this is to use loops in main to call all other functions, so that all calls return to main not another function, thus removing my exiting problem, but then again adding significant confusion, and size to my program. Is there possibly any other way of fixing this problem? Thank you very much for any assistance.

  2. #2
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    I'm not sure I understand the question. Could you post an example?
    Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In my programs I have a tendency to use recursion which I have learned is not good for large programs, and it seems to create problems exiting a program from within itself.
    That probably is due to an error in your implementation of the recursion rather than recursion itself. As for recursion being "not good for large programs", that is true in the sense that recursion requires the maintaining of a stack of values, some of which may be eliminated with iteration. On the other hand, a recursive solution may be easier to write and maintain than an iterative one, for certain problems.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    #include <mmsystem.h>
    #include <tchar.h>
    
    /* I know this is horribly written but it is an example */
    using namespace std;
    ULONG GetSongLength(LPCTSTR szFile);
    int age(int t_val, string temp);
    int display(int t_val, string temp);
    bool Exit = false;
    int main(int t_val, string temp) {
        if ( Exit == false ) {
        cout << "Enter your age: ";
        cin >> t_val;
        cin.ignore();
        temp = "";
        age(t_val, temp);
        }
       return 0;
    }
    
    int age(int t_val,string temp) {
        cout << "Okay enter your name: ";
        getline (cin, temp);
        cin.ignore();
        display(t_val, temp);
        cout << "SEE IT DOESNT EXIT IT RETURNS HERE" << endl;
        cin.get(); //after this it returns to main thus exiting, but it returns to calling function, so therefor
                  //exiting the program in this way does not work
    }
    
    int display(int t_val, string temp) {
        cout << "The age you entered: " << t_val << endl;
        cout << "The name you entered: " << temp << endl;
        Exit = true;
        cin.get();
        main(t_val, temp);
    }
    I hope that works for an example

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not permitted to call the global main() function from within the program. What you can do is place what main() does in another function, and call that function instead. You should avoid global variables such as Exit.

    EDIT:
    Erm, if you want to use arguments from the command line (and it appears that you don't), main() should be declared as:
    Code:
    int main(int argc, char* argv[])
    A main() that takes an int as the first argument and a string as the second is non-standard.
    Last edited by laserlight; 01-07-2007 at 02:32 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    how does that change it? Not saying it doesnt I justdo not see the immediate result. Also why are globals so bad?

  7. #7
    Registered User rynoon's Avatar
    Join Date
    Dec 2006
    Location
    London, ON
    Posts
    26
    Quote Originally Posted by Raigne
    how does that change it? Not saying it doesnt I justdo not see the immediate result. Also why are globals so bad?
    I'm going to echo laserlight's suggestion. There should only ever be one single instance of main. What it looks like you want to do here is have the program exit when it reaches the return statement of main. The problem is, your main function is called from within your display function, therefore existing only within the scope of that function. So the main is called, it returns a value (0) to your display function and the program continues to operate.
    Courage, my friends; 'tis not too late to build a better world. - Tommy Douglas

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    how does that change it? Not saying it doesnt I justdo not see the immediate result.
    Your code would then comply with the C++ Standard. That standard does not allow main() to be called, much less used recursively. In fact, your code would probably be better done without recursion at all.

    Also why are globals so bad?
    They can make maintenance and reasoning about your programs more difficult. For example, it may be difficult to predict what the state of a global variable will be after a function call, so changing a function may require changing how it deals with that global variable, which in turn may require changes to other functions, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Yes, sounds good, but I cant seem to figure out how to get it to work in such a way. Sad, I understand harder things than this, it is just baffling me... I have been programming for the last 2 hours a little program, no more than 50 lines, and I cant get it to work

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... okay, what are you trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Maybe this will help:
    Code:
    #include <iostream>
    int age(void); 
    void display(int t_val); 
    
    int main(void)
    {
       while ( int ageV = age() ) 
       {
    	display(ageV);
       }
       return 0;
    }
    
    int age(void) 
    {
    	int ageV = 0;
    	std::cout << "Okay enter your age (0 for exit): ";
    	std::cin >>ageV;
    	return ageV;
    }
    
    void display(int t_val) 
    {
    	std::cout << "The age you entered: " << t_val << std::endl;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    ...I just deleted the code to restart, started getting errors. Although I'm just trying to make the stupid thing exit without going back to the functions. I give up for tonight tho 3 am so im gunna get some sleep. Ill check posts in the morning before class to see what you guys say night.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Vart i understand your code fully, but would this approach be efficient in a situation where there any multiple function, possibly 10+. If it is the only way then I shall. Thanks for replies

    EDIT: Why do you give a void argument to age?
    Last edited by Raigne; 01-07-2007 at 04:14 PM.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Raigne
    Vart i understand your code fully, but would this approach be efficient in a situation where there any multiple function, possibly 10+. If it is the only way then I shall. Thanks for replies
    Sure it would. Why wouldn't it be?

    EDIT: Why do you give a void argument to age?
    In C++, it means the same as no arguments. Some people just prefer it this way.
    Also, in C, nothing between the parentheses actually means something different.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    oh, I see. As far as why wouldnt is the fact that is alot of while statements, and what if I need one function to use information retrieved during another would I just run both functions under the same while block?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM