English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello ,

I have wrote a very long c++ program that runs perfect when run it the first time. But at the end of it I ask the user if they want to run again. If they hit 'y' then it will call the main() function and run again. The problem is the program will redirect the user to my error() function because all the variables that the user choose the first time the program was ran are still being stored. Is there anyway I can flush all the vars used the first time , as if the program was being ran for the first time. Thanks , Chris

2007-02-20 04:00:53 · 6 answers · asked by chaessigbikerboy 2 in Computers & Internet Programming & Design

6 answers

I would replace the variables with a class, whose members are your variables from before. Then, just create a new instance of your class object whenever the program runs.

2007-02-20 04:06:04 · answer #1 · answered by Kryzchek 4 · 2 0

Dude , you are using C++ and it's object oriented ! Why don't you go for OOP approach to solve this problem ??
Better to create a class which is able to perform the actual tasks of the application with member functions and variables as per requirements .
Create a loop in main function which has the ability to create instances of your class . Create an instance of your class for the first time . When your work with the current class instance is over ( program executed for the first time ) then go and ask your user if he/she wants to continue the application . If yes , dispose/destruct your current class instance , make a new instance and repeat the program , otherwise break the main loop and finish the program .

2007-02-20 04:45:28 · answer #2 · answered by Mo 3 · 1 0

Calling the main() function directly to cause your program to re-run is generally considered a Bad Idea (tm) :). This is because the main function is the entry-point of your application and may (depending on the platform you are using) initialise all sort of things before your code is executed.

It would be better to structure your code in such a way that what you are doing is located in a separate function or class and will tidy up its own memory before finishing. Also making sure your variables are initialised to something sensible helps.

2007-02-20 04:09:51 · answer #3 · answered by Martin P 1 · 1 0

You can make a system call to run a program thru the OS, that program happens to be the same program. It's easy to do in Java, I suspect it is equally easy for C++.

I am talking in the way the minimal modification to the stuff you already wrote.

2007-02-20 05:03:01 · answer #4 · answered by Andy T 7 · 0 0

Why do you call main?

Without code, it is hard to understand what specifically you are talking about.

Sounds like you need to reorganize your program so that main calls sub functions to do the work. main() should not be called from within the program.

2007-02-20 04:06:33 · answer #5 · answered by Beam 3 · 1 0

enable say factorial n a million*2*3 ....*n int Facto(int val) { if(val <0 ) return 0; // look after against incorrect unfavorable fee else if (val == 0) return a million; // math rule, 0! is equivalent a million else return (val*Facto(val-a million)); // recursive call with the help of itself } inf actuality u have something like that val*(val-a million)* ....2*a million*a million final a million by using fact as I stated math rule, 0! is equivalent a million be conscious: recursive call might reason stack overflow by using fact the stack of function is loose, after the final era. Facto(Facto(Facto(Facto ........ ))))))) -> meaning recursive

2016-09-29 09:13:45 · answer #6 · answered by ? 4 · 0 0

fedest.com, questions and answers