From a beginner's stand point, could anyone explain how to call a section of code from another section? Here in my KidzTown Awana Inventory Database System, or KTAIDS for short, I have a section of code that is a calculator, which can be called from the main menu. That's fine and all, but how do I call the main menu from the calculator? (FYI: if you copy this and run it, the calculator can be accessed through the "NUMBER CALC" on the menu.) This is far from being finished, but please give me some tips on anything else. (I'm already aware of a security hole in SYSTEM(PAUSE);, so don't say anything about that.)
jloundy: C++ Standard, Clause 3.6.1, Paragraph 3:
The function main shall not be used [i.e., called] within a program.
OP:
You cannot "call" a general section of code (without resorting to gotos), but you can "call" a function. So each of your operations should be in its own function, including the menu.
What is the "copyright" stuff about? Don't post code here with that word in it. If it's truly copyrighted, you're on your own.
And although I suppose there is a security hole in system("pause") (meaning that a different program called "pause" could run), the main reason not to use it is that a simple cin.get() will work on all platforms as long as you've kept the input buffer free of newlines, which you should always do anyway.
Also, you should avoid "Hungarian" (Microsoft) notation for your variable names. It's ugly and virtually pointless. Nobody likes it.
int main() // don't declare params is you're not using them
{
while( true )
{
displayMenu();
switch( getChoice() ) // define getChoice to return only uppercase.
{
case'I':
doInventory(); // The general word "do" is suboptimal;
break; // however, I don't know what these do!
case'C':
doClerks();
break;
case'N':
doCalculator();
break;
case'X':
return 0;
default:
cout << "Invalid choice. Try again.\n";
}
}
}
Very simply: because main() is a "special function".
While technically legal in ANSI C, it is dangerous. For that reason, it is illegal in C++. The problem is that calling main() involves more than the usual call mechanism -- it is the system entry point into your program.
Further, recursion on the center function of a program consumes memory that is never released until the program is terminated and the system reclaims the program's resources. Such a blatant leak is considered poorly by those who have the hack nature.
In worst case scenarios, you could crash the program or smash memory important to the operation of your program -- depending on your system/compiler and the way that main() is initialized.
You could also use goto, though I've heard it should be avoided.
I don't really understand why. I've been playing around with assembler lately and similar commands are necessary there.
Could anybody explain this?
In a structured language (i.e. almost everything other than Assembly and Fortran), the only time using goto is acceptable is when not using it will make the code more difficult to understand. This has happened to me only two or three times, and yours is not one such case.
Hammurabi's example shows how to call functions, so I don't what you're asking.
EDIT: Where did the post go?
Oh, also, nobody ever said you can't post copyrighted code. I've done it at least once.
However, you may get into trouble if you post code that's supposed to be secret.
it WILL be copyrighted with my company name once i'm done with it.
In that case, my suggestions are copyrighted also.
Could anybody explain [why you should avoid goto]?
There is nothing inherently wrong with goto. The problem is that you can write unstructured (spaghetti) code with it, which gets very hard to maintain when a program gets large. As long as it is used in a structured manner, such as jumping to the end of a double loop, it is fine. But if there is a higher-level language mechanism available to do the same thing, then that is preferable.
You could also use goto, though I've heard it should be avoided.
When you use the goto function your code becomes like "spaghetti code" in that you're kind of jumping all over the place and it could become easy to lose where you are in your own program. I suppose you can also end yourself up in an infinite loop if written incorrectly. Then again you can do that with control structures lol.