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.

01-07-2007

rynoon

I'm not sure I understand the question. Could you post an example?

01-07-2007

laserlight

Quote:

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.

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
}

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.

01-07-2007

Raigne

how does that change it? Not saying it doesnt I justdo not see the immediate result. Also why are globals so bad?

01-07-2007

rynoon

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.

01-07-2007

laserlight

Quote:

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.

Quote:

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.

01-07-2007

Raigne

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

...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.

01-07-2007

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

EDIT: Why do you give a void argument to age?

01-07-2007

CornedBee

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?

Quote:

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.

01-07-2007

Raigne

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?