I'm really not happy with the way it's turning out

I've been working on this program for a good while now. I have 7 files, the main.cpp, three headers and three cpp files all included into the main.

I've created objects and passed them back and forth as refferences. yet when finally tying it all together in the main function i'm finding myself repeating code. Add to that, that I found that I woudln't be able to print an object from inside the class without including a header from a module that I really wanted to keep seperated from this module. I'm wondering if I need to start from scratch and re-design the whole thing.

I guess for now, let me ask this. The code that I'm repeating, includes a call to a function that I've got in another module, and I'm passing the refference of an object to it already. The easy way to handle code that repeats is to put it into a function that I can call repeatedly, however I'd have to pass the refference to this function. I realize that I can forward that refference again from inside the new function, but wonder if that means i'll have to use a double refference, e.g. currently i call getid(I_List &list). If i put this call into another function, would I have to make the getid take I_List &&list?

Here is what the main.cpp looks like:
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include "I_List.h"
#include "getid.h"
#include "RList.h"

using namespace std;

int main(int argc, char *argv[])
{
   I_List L_quill ("quill.dat"); // quill list
   I_List L_thick ("thickener.dat"); // thickener list
   I_List L_add ("additive.dat"); // additve list
   I_List L_parch ("parchment.dat"); // parchment list
   I_List L_ink ("ink.dat"); // ink list
   I_List L_misc ("miscellaneous.dat"); // misc list
   I_List L_char ("classes.dat"); // character class list

   vector<recipe> L_rcpe; // spell recipe list

   cout << "Welcome to the recipe builder." << endl;

   while (true)
   {
      cout << "1. Add a recipe" << endl;
      cout << "2. Save to file" << endl;
      cout << "3. Exit program" << endl;
      cout << "Option: ";

      int choice;
      cin >> choice;

      if (choice > 3 || choice < 1) // invalid option, re-prompt
      {
         cout << endl << "Invalid choice. Please select again." << endl << endl;
         continue;
      }

      if (choice == 3) // chose to exit.
      {
         // add code here to check if data is not saved and prompt if not.

         cout << "exiting..." << endl;
         return 0;
      }

      if (choice == 2) // chose to save file
      {
         // add code here to save L_rcpe to file (Note: use a function call for here and choice 3)
      }

      if (choice == 1) // chose to add a recipe
      {
         recipe Recipe; // create a new recipe

         string input; // string for input.
         int nimput; // for inputting numbers

         // First we get the Name of the resulting Spell
         // if we get a "-c" here, we cancel the add operation
         // and continue to the next iteration of the main loop.
         cout << "Type the Name of the resulting Spell: ";
         getline (cin, input); // get the input

         if (input == "-c")
            continue; // go to next iteration of main loop

         if (!input.empty())
            Recipe.set(input); // set the Name

         cout << "Name set to " << input << endl;

         // Second we get the Trivial of the recipe
         // if we get a -1 then we cancel this add operation
         // and continue to next iteration of main loop.
         cout << "Please enter the Trivial: ";

         cin >> nimput; // input trivial

         if (nimput == -1)
            continue; // go to next iteration of main loop

         Recipe.set(nimput); // set the trivial

         cout << "Trivial set to " << nimput << endl;

         // Third we enter all the classes and levels that can
         // use the spell, and insert each one until we get a
         // -1 which means no more classes. Note, no cancelling
         // add operation on this step.
         // (unless I look for no chr's being added.)
         cout << endl << "Character Classes / Levels" << endl;

         while (true)
         {
            cout << "Select Character Class" << endl;

            character chr; // make a character struct
            int cID = getid(L_char); // get a class id

            if (cID == -1) // if no id returned
               break; // exit this loop

            chr.charclass = cID; // set the class id

            cout << "Input required Level: ";
            cin >> nimput; // get required level

            chr.charlevel = nimput; // set level

            Recipe.add(chr); // add to recipe
         }

         // Fourth, we get the quill id, and quantity (which should always be 1)
         // if we get a -1 here, we cancel this add operation and continue to the
         // next iteration of the main loop.
         cout << endl << "Ingredients" << endl;
         {
            ingredient itm; // ingredient to add
            cout << "Select a Quill" << endl;

            int iID = getid(L_quill); // get a quill id

            if (iID == -1) // if no id returned
               continue; // go to next iteration of main loop

            itm.id = iID; // set item ID

            cout << "Enter quantity: ";
            cin >> nimput; // get quantity

            if (nimput <= 0) // if no quantity
               continue; // go to next iteration of main loop

            itm.qty = nimput; // set quantity

            Recipe.add(itm,quill); // Add quill to recipe
            int indx = L_quill.I_getindex(iID,0); // get index of quill from list
            cout << "Adding " << nimput << " " << L_quill.I_getitem(indx).name << endl;
         }

         // Fifth, we get the thickener id, and quantity (which should always be 1)
         // if we get a -1 here, we cancel this add operation and continue to the
         // next iteration of the main loop.
         {
            ingredient itm; // ingredient to add
            cout << "Select a Thickener" << endl;

            int iID = getid(L_thick); // get a thickenr id

            if (iID == -1) // if no id returned
               continue; // go to next iteration of main loop

            itm.id = iID; // set item ID

            cout << "Enter quantity: ";
            cin >> nimput; // get quantity
 
            if (nimput <= 0) // if no quantity
               continue; // go to next iteration of main loop

            itm.qty = nimput; // set quantity

            Recipe.add(itm,thickener); // Add Thickener to recipe
            int indx = L_thick.I_getindex(iID,0); // get index of thickener from list
            cout << "Adding " << nimput << " " << L_thick.I_getitem(indx).name << endl;
         }

         // Sixth, we get the additive id, and quantity (which should always be 1)
         // if we get a -1 here, we cancel this add operation and continue to the
         // next iteration of the main loop.
         {
            ingredient itm; // ingredient to add
            cout << "Select an Ink Additive" << endl;

            int iID = getid(L_add); // get an additive id

            if (iID == -1) // if no id returned
               continue; // go to next iteration of main loop

            itm.id = iID; // set item ID

            cout << "Enter quantity: ";
            cin >> nimput; // get quantity

            if (nimput <= 0) // if no quantity
               continue; // go to next iteration of main loop

            itm.qty = nimput; // set quantity

            Recipe.add(itm,additive); // Add Ink Additive to recipe
            int indx = L_add.I_getindex(iID,0); // get index of additive from list
            cout << "Adding " << nimput << " " << L_add.I_getitem(indx).name << endl;
         }

         // Seventh, we get the parchment id, and the quantity (which should always be 1)
         // if we get a -1 here, we cancel this add operation and continue to the
         // next iteration of the main loop.
         {
            ingredient itm; // ingredient to add
            cout << "Select a Parchment" << endl;

            int iID = getid(L_parch); // get a parchment id

            if (iID == -1) // if no id returned
               continue; // go to next iteration of main loop

            itm.id = iID; // set item ID

            cout << "Enter quantity: ";
            cin >> nimput; // get quantity

            if (nimput <= 0) // if no quantity
               continue; // go to next iteration of main loop

            itm.qty = nimput; // set quantity

            Recipe.add(itm,parchment); // Add Parchment to recipe
            int indx = L_parch.I_getindex(iID,0); // get index of parchment from list
            cout << "Adding " << nimput << " " << L_parch.I_getitem(indx).name << endl;
         }

         // Eighth, we get the ink id (multiple like we did with chars above) and
         // their quantities (can be more than 1, but at least 1). a -1 here simply
         // means the end of inks. Note: no exiting out of this step unless I look for
         // no inks being added.

         // Ninth, we get any miscellaneous items here. Use loop like chars and inks here.

         // Tenth, we show the entire recipe to the user and prompt for an accept or cancel.
         // an accept and this recipe get's added to the vector L_rcpe. a cancel simply
         // continues to next iteration of main loop.
         // Note: Make a function to print recipe data, passing cout as &ostream. Can re-use
         // said function for writing all the recipies to a file from above save options.
      }
   }

   system("PAUSE");
   return EXIT_SUCCESS;
}
As you can see, it's getting rather lengthy.. more so than I think it needs to be. So If i make a function or two for adding the stuff to the Recipe, and pass it the I_List refference, would forwarding that refference to getid() require a ** pointer, or getid(I_List &&list) ??
Topic archived. No new replies allowed.