Call a section of code from another!

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
****************************************
********KidzTown Awana Inventory******** 
************Database System*************
****************************************
******Copyright (C) Blake Burgess*******
****************************************
*************June 15, 2009**************
****************************************
*/

//==========UNFINISHED!==========

#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
  char mainMenu;
///////////////////////////////////////////////////////////////////////////////  
  double dNCalc1 = 0.0;
  char NCalcOp;
  double dNCalc2 = 0.0;
  double dNCalcOut = 0.0;
///////////////////////////////////////////////////////////////////////////////
  cout << "++==========================================================++\n";
  cout << "||                                                          ||\n";
  cout << "||  KK  KK   TTTTTT     AA     IIIIII   DDDD       SSSS     ||\n";
  cout << "||  KK KK      TT     AA  AA     II     DD  DD    SS   S    ||\n";
  cout << "||  KKKK       TT     AAAAAA     II     DD   DD     SS      ||\n";
  cout << "||  KK KK      TT     AA  AA     II     DD  DD    S   SS    ||\n";
  cout << "||  KK  KK     TT     AA  AA   IIIIII   DDDD       SSSS     ||\n";
  cout << "||                                                          ||\n";
  cout << "++==========================================================++\n";
  cout << "||                                                          ||\n";
  cout << "||   *  *  *   KTAIDS Database Version 1.0.1.0   *  *  *    ||\n";
  cout << "||   Copyright (c) 2009 Blake Burgess All Rights Reserved   ||\n";
  cout << "||                                                          ||\n";
  cout << "++=============++=============++=============++=============++\n";
  cout << "||     (I)     ||     (C)     ||     (D)     ||     (N)     ||\n";
  cout << "||  Inventory  ||    Clerks   || Departments || Number Calc ||\n";
  cout << "++=============++=============++=============++=============++\n";
  cout << "||     (B)     ||     (V)     ||     (P)     ||     (X)     ||\n";
  cout << "|| Begin C.Out ||  View Logs  || Preferences ||  Terminate  ||\n";
  cout << "++=============++=============++=============++=============++\n";
  cout << "||                                                          ||\n-->>\t";
  cin  >> mainMenu;
  
  if (mainMenu == 'i' ||
      mainMenu == 'I')
  {
      return 0;
  }
  if (mainMenu == 'c' ||
      mainMenu == 'C')
  {
    return 0;
  }
  if (mainMenu == 'd' ||
      mainMenu == 'D')
  {
      return 0;
  }
/*Code for the Number Calculator on the Main Menu.*/
  if (mainMenu == 'n' ||
      mainMenu == 'N')
  {
    cout << "||                                                          ||\n";
    cout << "++==========================================================++\n";
    cout << "||                                                          ||\n";
    cout << "||    Type a negative number to return to the main menu.    ||\n";
    cout << "||                                                          ||\n";
    cout << "++==========================================================++\n";
    cout << "||             INSERT A POSITIVE DECIMAL NUMBER             ||\n";
    cout << "||                                                          ||\n-->>\t";
    cin  >> dNCalc1;
    cout << "||             INSERT A VALID OPERATOR (*,+,-,/)            ||\n";
    cout << "||                                                          ||\n-->>\t";
    cin  >> NCalcOp;
    cout << "||             INSERT A POSITIVE DECIMAL NUMBER             ||\n";
    cout << "||                                                          ||\n-->>\t";
    cin  >> dNCalc2;
    cout << "||                                                          ||\n";
    cout << "||                                                          ||";
    //Calculate input values
    if (NCalcOp == '*')
    {
      dNCalcOut = dNCalc1 * dNCalc2;
    } 
    
    if (NCalcOp == '+')
    {
      dNCalcOut = dNCalc1 + dNCalc2;
    }
    
    if (NCalcOp == '-')
    {
      dNCalcOut = dNCalc1 - dNCalc2;
    }
    
    if (NCalcOp == '/')
    {
      dNCalcOut = dNCalc1 / dNCalc2;
    }
    //display output
    cout << "\n-->>   ";
    cout << dNCalc1;
    cout << "\n-->>  ";
    cout << NCalcOp;
    cout << dNCalc2;
    cout << "\n-->>----------------------------------------------------------";
    cout << "\n-->>   ";
    cout << dNCalcOut;
    cout << "\n";
    cout << "||                                                          ||\n";
    cout << "++==========================================================++\n";
    cout << "||                                                          ||\n";
    cout << "-->>  ";
    system("PAUSE");
    return 0;
    cout << "||                                                          ||\n";
     
    }
    
  if (mainMenu == 'v' ||
      mainMenu == 'V')
      {
          string line;
          ifstream logfile ("data\\log.dat");
          if (logfile.is_open())
          {
            while (! myfile.eof() )
            {
              getline (myfile,line);
              cout << line << endl;
            }
            myfile.close();
          }
    else cout << "Unable to open file";
      }
  }
  
  if (mainMenu == 'x' ||
      mainMenu == 'X')
  {
      return 0;    
  }
  
  cout << "||                                                          ||\n";
  cout << "||  That is not a valid command. KTAIDS will now terminate. ||\n";
  cout << "||                                                          ||\n";
  cout << "++==========================================================++\n\n-->>\t";
  system("PAUSE");	
  return 0;
}
Functions.
your should really use functions , but you could recall main() to go backto the beginning of the program, which is the menu.
main()
but you could recall main() to go backto the beginning of the program, which is the menu.
Few things fuck so much with the logic of a program like recursing main().
Never, ever recurse main().
thats what i hear, can u explain why though?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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";
        }
    }
}

Last edited on
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.

Hope this helps.
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.


All code is copyrighted. Even open source code. See the GPL for an example

Also, you should avoid "Hungarian" (Microsoft) notation for your variable names. It's ugly and virtually pointless. Nobody likes it.


I like it ;_;
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?
sorry about the copyright stuff Hammurabi, but it WILL be copyrighted with my company name once i'm done with it.
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.
Last edited on
i think i skipped over Hammurabi's code on accident...oops...

by the way, what are Hungarian (Microsoft) notations?
Last edited on
Well, if you don't want to ask them, you should probably try Google first.
http://en.wikipedia.org/wiki/Hungarian_notation

(I have no idea what Hammurabi meant by "(Microsoft)".)
Last edited on
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.
Last edited on
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.

Edit ** ahh to slow for Hammurabi :(
Last edited on
thanks!

this topic is now officially closed.
Topic archived. No new replies allowed.