Structs, arrays, omitting vectors

Hi. I'm doing an assignment in C++ that mainly deals with structs and arrays. They are not my strong point right now. I constructed this code below with a friend who is MIA at the moment. The code outputs what I want, but he included vectors. I am not allowed to use vectors yet since I barely began OOP, so there are some functions that I am unsure of adjusting/omitting correctly. I tried to abbreviate it somewhat for reading ease, so hopefully it still makes sense.


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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;

struct menuItemType
{
	string menuItem;
	double menuPrice;
};

void getData(menuItemType menuList[]);
void showMenu(menuItemType menuList[], int len);
void printCheck(vector<menuItemType> menuOrder);

int main()
{
	// Declarations
	const int MENU_LENGTH = 8;
	menuItemType menuList[MENU_LENGTH];
	vector< menuItemType > menuOrder;
	int orderChoice = 0;
	bool ordering = true;
	int count = 0;
	// Set menu

	getData(menuList);
	// Order
	showMenu(menuList, MENU_LENGTH);

	cout << "Enter the number for the item you would\n"
		 << "like. Enter -999 to complete ordering.\n"
		 << endl;

	while (ordering)
	{
		cin >> orderChoice;

		if (orderChoice != -999 && orderChoice <= MENU_LENGTH)
		{
			menuOrder.push_back(menuList[orderChoice - 1]);
		// I'm not sure what to substitute push_back with here...
                }

		else
			ordering = false;
	}

	printCheck(menuOrder);
	return 0;
}

void getData(menuItemType menuList[])
{
	// Declare menuItemTypes

	.
        .
        .

	// Initialize menuItemTypes variables

	
	.
        .
        .

	menuList[0] = plainEgg;
	menuList[1] = baconEgg;
	menuList[2] = muffin;
	menuList[3] = frenchToast;
	menuList[4] = fruitBasket;
	menuList[5] = cereal;
	menuList[6] = coffee;
	menuList[7] = tea;

}

void showMenu(menuItemType menuList[], int len)
{
	// Function variables
	int count;

	cout << "Welcome to Johnny's Restaurant.\n"
		 << "What would you like for breakfast?\n" << endl;

	cout << showpoint << fixed << setprecision(2);

	for (count = 0; count < len; count++)
	{
		cout << left << "[" << count + 1 << "] "
			 << menuList[count].menuItem << ' ' << '$'
			 << menuList[count].menuPrice << endl;
	}
}


void printCheck(vector<menuItemType> menuOrder)

{
	double billTotal = 0;
	double billTax = 0;
	const double TAX = .05;

	cout << "Thank you for chosing Johnny's Restaurant!\n"
		 << "Customer check:\n" << endl;

	cout << showpoint << fixed << setprecision(2);

	for (int i = 0; i < menuOrder.size(); i++) //use new variable?
	{
		cout << setw(14) << left << menuOrder[i].menuItem
			 << '$' << menuOrder[i].menuPrice << endl;
		billTotal += menuOrder[i].menuPrice;
	}

	billTax = billTotal * TAX;
	billTotal += billTax;

	cout << setw(14) << left << "Tax" << "$" << billTax << endl
		 << setw(14) << left << "Total" << "$" << billTotal << endl;
}
Other than by writing your own vector, this task can be done by declaring a max size and passing the current size to any function that uses the array:
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
#include <iostream>

void printNumbers( const int* numbers, const int current_size )
{
  for ( int i = 0; i < current_size; ++i )
    printNumber( numbers[ i ] );
}

int main( void )
{
  const int MAX_SIZE = 16;
  int numbers[ MAX_SIZE ];
  int current_size = 0;

  while ( /* the user wants to add a number*/ )
  {
    if ( current_size < MAX_SIZE )
    {
      numbers[ current_size ] = a_function_returning_an_int();
      ++current_size;
    }
    else
    {
      // handle the fact that there is no space
      // at this point, just say that there isn't space and quit the loop
    }
  }
  

  printNumbers( numbers, current_size );

  return 0;
}

Last edited on
Aside from the conspicuous typo's Lowest0ne about has your answer. Another solution would be dynamically allocating the array of orders with the 'new' operator. I'm self taught and I often forget in what progression universities traditionally teach stuff in. But I would assume that the STL containers would precede dynamic allocation in any reasonable setting that focused on OOP and RAII. So I'll only post my suggestion upon request.
I found two typos, any more?
Last edited on
Re-examine Line 6 my friend.
Yeah, before you noted the typos I had that function named printNumber, so a little misleading indeed.

I originally had used jksdk4's class names and didn't want to write the code to print a single menuItemType ( printMenuItemTypes() calls printMenuItemType() many times ). Then I decided that it would be more of a learning exercise if jksdk4 couldn't just do a copy/paste and switched it to int.

So thats the story of that post. :)
Last edited on
Topic archived. No new replies allowed.