Point me in the right direction???

deleted
Last edited on
Well, since the menu must be read from a file, you cannot assume that there will always be 8 menu items. You cannot hardcode them as you do in lines 38+.

So, lets first correct that part. Since the number of items is varied, you should use a dynamic array allocated in the heap. You should use the STL's vector class (http://www.cplusplus.com/reference/stl/vector/).

To read the file, use an fstream object and then the getline() function (http://www.cplusplus.com/reference/string/getline/). As instructed in the instructions, these corrections really go into a separate function called getData(). (I HATE camel casing for functions).

I see that you added an item to the struct: numOrdered. You are mixing contents here. I would declare a separate struct:

1
2
3
4
5
struct OrderedItem
{
    MenuItemType menuItem;
    int numOrdered;
};


I would then use a vector<OrderedItem> to maintain the items ordered. This vector would then be used by the function printCheck() whenever the check is needed.
thank you for taking your time in this topic.

I am not very familiar with the dynamic arrays, can you please write an example.
And also i do not understand what you mean by allocated in the heap?

Thank you again in advance
Without delving too much into it, there are two "places" where you can allocate memory: The stack and the heap. Stack memory is allocated by the variables created by the programmer. If a varied number of elements is later needed, the place to get more memory is the heap. In C++, a dynamic array looks like:

1
2
3
4
5
6
7
8
int totalItems = 0;
cout << "Enter the total number of iterms:  ";
cin >> totalItems;
int *myArray = new int[totalItems];
for (int i = 0; i < totalItems; i++)
{
    myArray[i] = i;
}


That is the basics. You need a pointer that points to the first element of the array, and you need a variable to hold the total number of elements.

The above requires that you free the allocated memory (by means of the new operator) using the delete[] operator. If you pointer variable goes out of scope, you must ensure you freed the memory used by that pointer before that happened.

Stuff like memory leaks have pushed the STL to create what is usually called collections. The vector class is a basic form of collections. There are other, more ellaborated classes that serve higher purposes. For example there is map, which is a dictionary collection. There are others as well. Don't ask me about STL. I am no good at it.

If the above is just over your head, consider stepping back and start with the basics of programming and C++.
Topic archived. No new replies allowed.