Sunday, 15 January 2012

C++ restart main without losing variables -



C++ restart main without losing variables -

my program's main function displays switch menu. when alternative 1 entered, function called "shuffles" array of "cards". after shuffling complete, function returns programme origin calling main(), menu shown again.

the problem have alternative 4 of menu writes shuffled array file. when cards shuffled , programme restarted, array info lost, , hence outputted file junk. there way restart main() without info beingness lost?

i in class , limited in tools can use, basic code acceptable. basically, i'm looking goto little safer (goto, way, forbidden in class).

it's not thought c++ programme phone call own main function. in fact, leads undefined behavior, means have no guarantees behavior of program. crash, or proceed corrupt data, or format hard drive, etc. (that lastly 1 unlikely, though).

i think reflects more fundamental problem how programme works. if want info persist across functions, need place info somewhere won't clobbered other functions. example, set info in heap, pass pointers info around. or, define local variable in main, pass downwards functions , have functions homecoming when they're done. consider making objects representing data, passing objects across different functions.

without seeing code, uncertainty can give more specific reply this. when designing programs, maintain info flow in mind. think info needs go , how you're going there.

hope helps!

c++ main

No comments:

Post a Comment