Confusions with structures

Pages: 12
I am writing a program that will automate a diner, so have the user order as many items as they want and they with that information it will print a receipt and calculate the balance due. I have a few concerns or questions about the set up. I am supposed to use structures and arrays to store information from the customer, so from what i gathered i need to have a structure with information in it that corresponds to the information i need and then take that structure and make an array out of it for each customer that uses the program. If i had any code that made any sense i would post it but i feel like just asking the question head on is a better way for me to understand the situation. Mainly i just need to know how to pass information into the structure from other functions in the code and then use that information to actually print out a receipt for each order. My train of thought right now is this:

Introduce the code
Gather information about menu using a function
(code is supposed to be able to have 100 customer)
while CustomerNumber < 100
Print out meneu ( using function)
(menu will have numbers so the customer can order)

maybe if statements inside the loop so the user can pick their items
something like
if(order = 1) { "you chose ( whatever item corresponds to array of foods)
if answer == y || Y ( asks user if they want to order again)
}

some how take that information and use a structure to save all the information such as : Customer order # 1-100 , Customer order ( the name of food and price)
then using that information print a bill and calculate the tax

then since its a while loops repeat until it hits 100


I feel like its really not too hard of a thing to figure out but piecing it all together is just some thing that my brain cant wrap around rn, also doesn't help that i have two people in my group doing different functions so its hard for me to figure out information using dummy variables.

I find it easiest to start by defining the data that you need. Then define the code that will us the data.

The menu is a collection of items. each item has a name and a price. So
1
2
3
4
struct MenuItem {
    string name;
    double price;
};


The menu is an array or vector of these;
vector<MenuItem> menu;

A customer orders items from the menu. They can order several of the same item. Let's identify which item by the index into the menu vector. So maybe:
1
2
3
4
struct OrderItem {
    int menuItemIndex;  // index of item in the menu
    unsigned quantity;   // how many
};


Now we can define a customer. A customer orders several items. You have to print the receipt and calculate the total due
1
2
3
4
5
struct Customer {
    void printReceipt(std::ostream &os);
    double calcTotal();
    vector<OrderItem> order;
};


And finally all the customers in the restaurant is
vector<Customer> customers;

Once you have a draft of the data, the code starts to become a little clearer. You'll probably find that you modify the data as more details emerge, but this is a good starting point.
how would i do this with arrays, mainly i don't know how to use vectors and we wont be able to use vectors for this if i even wanted to
A vector is like an array, but it also knows how many items are in it.

You know that there can be at most 100 customers:
1
2
3
constexpr unsigned Maxclients {100};
Customer customers[Maxclients];
unsigned clients = 0; // number of customers that have visited us 


You know how many MenuItems you have:
1
2
constexpr unsigned Maxitems {7};
MenuItem menu[Maxitems];



How many orders one Customer can make/remember? The vector would be flexible. The array has fixed size. Therefore, there are at least two approaches:

1. The Customer has some upper limit N:
1
2
3
4
5
struct Customer {
    const unsigned N {3};
    OrderItem order[N];
    unsigned orders {0};
};

Now the customer gets "sorry, no more" if they try to order more than N times. For example:
3 Coffees
1 Cake
2 Coffees
makes 3 and no more is allowed.

2. Maxitems orders. This is more complex, for you have to check with each order whether same itemtype has already been ordered. In the above example the second batch of coffee has to be added to the earlier order, i.e. after third order the customer has only two entries:
5 Coffees
1 Cake
What is unsigned, and is it any different than any other variable?
Also, The code that ive worked out is kinda something like this, there are two user functions that im not working on so just believe that they work. I wonder if a approach like this is possible, and if it is all i need to know is how to take information about the user like their order and the price of their order and then print a check for them. I know i need an array and im assuming it needs to be an array of structures, but how would i go about doing 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
//David Wood 
//December 4,2018
//Automated Diner menu
// This program will automate the ordering and billing of a diner. It will print the menu onto the screen
// and have the user chose how many times they want to order, then it will total the price and print a receipt


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Structure.h"
using namespace std;

void Introduction();
void printCheck();

int main() {
	int CustomerNumber = 0;
	char Y_N = 'y';
	int OrderNumber;
	menuItemType menuList[8];


	//getData(); // Function from another classmate. gets menu information from a file and puts information into arra

	Introduction(); // calls the introduction

	while (CustomerNumber < 100) { // can only have 100 customers
		//showMenu(); // User function that prints out menu informtaion for the user to chose order

		while (Y_N == 'y' || 'Y') { // While the customer wants to order 
			switch (OrderNumber) {
			case 0:
				cout << "You chose : " << menuList[0].menuItem << "..........." << menuList[0].menuPrice;
				cout << " Would you like to order again? " << endl;
				cin >> Y_N;
				if (Y_N == 'n' || 'N')
					break;
			case 1:
				cout << "You chose : " << menuList[1].menuItem << "..........." << menuList[1].menuPrice;
				cout << " Would you like to order again? " << endl;
				cin >> Y_N;
				if (Y_N == 'n' || 'N')
					break;
			case 2:
				cout << "You chose : " << menuList[2].menuItem << "..........." << menuList[2].menuPrice;
				cout << " Would you like to order again? " << endl;
				cin >> Y_N;
				if (Y_N == 'n' || 'N')
					break;
			case 3:
				cout << "You chose : " << menuList[3].menuItem << "..........." << menuList[3].menuPrice;
				cout << " Would you like to order again? " << endl;
				cin >> Y_N;
				if (Y_N == 'n' || 'N')
					break;

			}
		}


		//	printCheck();

		CustomerNumber++;
	}





	system("pause");
	return 0;
}

//David Wood 
//December 4,2018
//Introduction
//The intro to the code

void Introduction() {
	cout << "Hello, Welcome to the diner. This program will automate the ordering and billing process" << endl
		<< "The menu will be printed onto the screen for you to pick your order, you will be able to order" << endl
		<< "as much as you like, the code will then print a receipt for you to pay with" << endl;
}

//David Wood 
//December 4,2018
//Printcheck
//Function to print the final check for each customer
void printCheck() {

}



Structure

1
2
3
4
5
6
7
8
9
10
#pragma once
#include <string>
using namespace std;

struct menuItemType {
	string menuitem;
	double menuPrice;

};
You don't need that switch statement. You could replace the entire switch statement with:

1
2
3
4
5
6
7
8
                cout << "You chose : " << menuList[OrderNumber].menuItem << "..........."\
 << menuList[OrderNumber].menuPrice;
                cout << " Would you like to order again? " << endl;
                cin >> Y_N;
                if (Y_N == 'n' || 'N')
                    break;

                }


if (Y_N == 'n' || 'N')
This doesn't do what you think it does. If you want to compare Y_N to two values then you have to do it explicitly: if (Y_N == 'n' || Y_N == 'N') Actually, you don't need the if statement at all since the while() condition does the same check (and has the same bug).

Since you want the loop to run at least once, use a do/while loop instead of a while loop.

So with all these changes, the loop looks like:
while (CustomerNumber < 100) { // can only have 100 customers
do {
//showMenu(); // User function that prints out menu informtaion\
for the user to chose order

cout << "You chose : " << menuList[OrderNumber].menuItem
<< "..........."
<< menuList[OrderNumber].menuPrice;
cout << " Would you like to order again? " << endl;
cin >> Y_N;
} while (Y_N == 'y' || Y_N == 'Y'); // While the customer wants to \
order


Now look at CustomerNumber. It defined at line 19, used in the while look at line 29 and incremented at line 65. That's a whole lot of distance for a variable that controls a simple counted loop. You can replace all of those with
for (int CustomerNumber=0; CustomerNumber < 100; ++CustomerNumber) {

Add some temporary code to printCheck() and remove the comment where it's called. Do the same for showMenu(). In other words, you want to be able to test the code that you've written.

Making these changes, the meat of the program looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    for (CustomerNumber = 0; CustomerNumber < 100; ++CustomerNumber) {
        do {
            showMenu(); // User function that prints out menu informtaion for t\
he user to chose order

            cout << "You chose : " << menuList[OrderNumber].menuItem
                << "..........." << menuList[OrderNumber].menuPrice;
            cout << " Would you like to order again? " << endl;
            cin >> Y_N;
        } while (Y_N == 'y' || Y_N == 'Y');      // While the customer wants to\
 order

        printCheck();

    }


How does OrderNumber get set?
I forgot to put it in the original code but to set order number it will ask the user through a cin. so before the couts it will ask them to chose their order.

What i need to know now is how do i use print check efficiently. I need information to be stored into a structure i'm assuming but how would i be able to do that if there are multiple orders. Or just in general how would i then take the information that the user gives me and then prints a check with all of their order and the prices combined into one function?
to set order number it will ask the user through a cin.

Okay. It looked like you might be prompting for order number inside showMenu(), in which case you would probably want showMenu to return the order number.

To print the order, you will have to store each the order number of each thing the customer orders in an array. You will also have to remember how many items are in the array. Then you can print the orders by going through the array and printing the appropriate info for each item.
okay, so heres where im having trouble. i just set up a structure called customerInfo. So inside that structure im going to need to have information for their order and information about how many customers there are. So both of those need to be arrays, if im not mistaken.
so it would be int order[20] ( just so there is enough room for the order) and then Customer[99] ( cause there is 99 customers. ) so i would need to cin Customer[CustomerNumber].order[] and then order would have to some how increment each time the customer wants to order again, if im thinking about this in the right way?
You have the right idea but do you really need to store all customer orders? Once you've processed one customer, why do you need to remember their order? It looks to me like when you're done printing their order, you're done with the customer.
I need to remeber their order up until i print their receipt. i need to know how many times they ordered and what they ordered so i can calculate what they owe and calculate the tax of it all. if i didnt have their information saved would i be able to calculate the price of their order if they ordered more than one or two things. cause i cant think of a way to figure it out like that.
Right, you need to know the order for the current customer - the items they ordered and how many. But once you have taken their order and printed it, do you still need to remember it? When you're taking the order for customer #17, do you still need to remember the order from customer #4? My point is that I think you can have just one instance of Customer.
Yeah i think you're right. I have a structure set up with a few different things
1
2
3
4
5
6
7
8
9
10
#pragma once
#include <string>
using namespace std;

struct CustomerInfo {
	int order;
	menuItemType MenuList[10];
	int counter;
	int CustomerNumber;
}Customer[99];

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
//David Wood 
//December 4,2018
//Automated Diner menu
// This program will automate the ordering and billing of a diner. It will print the menu onto the screen
// and have the user chose how many times they want to order, then it will total the price and print a receipt


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Structure.h"
#include "CustomerInformation.h"

using namespace std;

void Introduction(); // Prototype for Introduction
void printCheck();	 // Prototype for PrintCheck
void showMenu();


int main() {
	int CustomerNumber;
	char Y_N = 'y';
	int OrderNumber;
	


	//getData(); // Function from another classmate. gets menu information from a file and puts information into arra

	
	
	for (CustomerNumber = 0; CustomerNumber < 100; CustomerNumber++) {
		Introduction(); // calls the introduction
		cout << endl << endl << endl;
		cout << "Your order will be order number: " << CustomerNumber << endl;
		cout << endl;
		do {
			
			showMenu(); // User Function That prints out meneu information

			cout << "What would you like, You will be able to order more than once ( Input one order at a time) " << endl;
			cin >> Customer[CustomerNumber].order;

			cout << "You chose " << MenuList[Customer[CustomerNumber].order].menuItem
				<< "......" << MenuList[Customer[CustomerNumber].order].menuPrice << endl;
			cout << " Would you like to order again (enter 'y,Y' or 'n,N )" << endl;
			cin >> Y_N;

		} while (Y_N == 'y' || Y_N == 'Y'); // while the customer wants to order 
		

		printCheck();// Prints the check

		// Somehow clear the screen
	}





	system("pause");
	return 0;
}

//David Wood 
//December 4,2018
//Introduction
//The intro to the code

void Introduction() {
	cout << "Hello, Welcome to the diner. This program will automate the ordering and billing process" << endl
		<< "The menu will be printed onto the screen for you to pick your order, you will be able to order" << endl
		<< "as much as you like, the code will then print a receipt for you to pay with" << endl;
}

//David Wood 
//December 4,2018
//Printcheck
//Function to print the final check for each customer
void printCheck() {
	cout << "Here is your check please come again soon" << endl;

		
}

void showMenu() {
	cout << setfill(' ') << setw(20) << "DCC Diner"<< endl;
	for (int i = 0; i < 2; i++) {
		cout << i <<": " << MenuList[i].menuItem << ".........." << MenuList[i].menuPrice << endl;
	}
}



This is what i have, the only function i need to do now is the print. there are a few things i still need to do in order to get there though, I need a counter to count the amount of orders that the user wants, and then from there i need to reference the orders the prices and how many orders there are to calculate the check
Last edited on
Okay, so they order something. You store it in Customer[CustomerNumber].order. When they order the next item, you store it in... the same place.

I think Customer should be:

1
2
3
4
5
struct CustomerInfo {
	int orderItems[10];    // items ordered
	int counter;
	int CustomerNumber;
}Customer[99];


Then the loop is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do {
	int order;	// store the order here to reduce the typing.
	showMenu(); // User Function That prints out meneu information

	cout << "What would you like, You will be able to order more than once ( Input one order at a time) " << endl;
	cin >> order;
	cout << "You chose " << MenuList[order].menuItem
		<< "......" << MenuList[order].menuPrice << endl;

	Customer[CustomerNumber].orderItems[Customer[CustomerNumber].counter] = order;

	Customer[CustomerNumber].counter++;
	cout << " Would you like to order again (enter 'y,Y' or 'n,N )" << endl;
	cin >> Y_N;

} while (Y_N == 'y' || Y_N == 'Y'); // while the customer wants to order  
okay, that makes sense, i changed up the file according to that, so now i need to write my printcheck function. right now the code looks like :
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include <string>
using namespace std;

struct menuItemType {
	char menuItem[55];
	double menuPrice;

};

menuItemType MenuList[2] = { {"Eggs", 1.99},{"Bacon and Eggs", 2.99} }; // test array without partners work 


1
2
3
4
5
6
7
8
9
#pragma once
#include <string>
using namespace std;

struct CustomerInfo {
	int orderItems[10];
	int counter;
	int CustomerNumber;
}Customer[99];



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
//David Wood 
//December 4,2018
//Automated Diner menu
// This program will automate the ordering and billing of a diner. It will print the menu onto the screen
// and have the user chose how many times they want to order, then it will total the price and print a receipt


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Structure.h"
#include "CustomerInformation.h"

using namespace std;

void Introduction(); // Prototype for Introduction
void printCheck(CustomerInfo);	 // Prototype for PrintCheck
void showMenu();
	int CustomerNumber;
	char Y_N = 'y';
	int OrderNumber;


int main() {
	


	//getData(); // Function from another classmate. gets menu information from a file and puts information into arra

	
	
	for (CustomerNumber = 0; CustomerNumber < 100; CustomerNumber++) {
		Introduction(); // calls the introduction
		cout << endl << endl << endl;
		cout << "Your order will be order number: " << CustomerNumber << endl;
		cout << endl;
		Customer[CustomerNumber].counter = 0;
		do {
			int order;
			showMenu(); // User Function That prints out meneu information

			cout << "What would you like, You will be able to order more than once ( Input one order at a time) " << endl;
			cin >> order;

			cout << "You chose " << MenuList[order].menuItem
				<< "......" << MenuList[order].menuPrice << endl;
			
			Customer[CustomerNumber].orderItems[Customer[CustomerNumber].counter] = order;
			
			Customer[CustomerNumber].counter++;
			cout << " Would you like to order again (enter 'y,Y' or 'n,N )" << endl;
			cin >> Y_N;
			
		} while (Y_N == 'y' || Y_N == 'Y'); // while the customer wants to order 
		

		printCheck(Customer[CustomerNumber]);// Prints the check

		// Somehow clear the screen
	}


	system("pause");
	return 0;
}

//David Wood 
//December 4,2018
//Introduction
//The intro to the code

void Introduction() {
	cout << "Hello, Welcome to the diner. This program will automate the ordering and billing process" << endl
		<< "The menu will be printed onto the screen for you to pick your order, you will be able to order" << endl
		<< "as much as you like, the code will then print a receipt for you to pay with" << endl;
}

//David Wood 
//December 4,2018
//Printcheck
//Function to print the final check for each customer
void printCheck(CustomerInfo ThisOrder) {
	cout << "Here is your check, Please come again!" << endl;
	for (int i = 0; i < Customer[CustomerNumber].counter; i++) {
		 
		
	}

		
}

void showMenu() {
	cout << setfill(' ') << setw(20) << "DCC Diner"<< endl;
	for (int i = 0; i < 2; i++) {
		cout << i <<": " << MenuList[i].menuItem << ".........." << MenuList[i].menuPrice << endl;
	}
}



what i need to do is take the information that i stored in the array of orders and use that to print out what the user ordered and how much it costs and then tally up the cost. would i still need menuItemType MenuList[10]; inside the second structure or does the order array do the same thing when it comes down to printing out the stored information?
You're passing the customer info into printCheck, so you don't need to worry about Customer[CustomerNumber] at all.
I think's something like:
1
2
3
4
for (int i=0; i<ThisOrder.counter; ++i) {
    cout << MenuList[ThisOrder.orders[i]].menuItem << '\t'
         << MenuList[ThisOrder.orders[i]].menuPrice << '\n';
}


is adding the total for the order as simple as i think it is or is there a certain procedure to take to add stuff up?
Nope, it's as simple as you think it is. Just be sure to initialize your "sum" variable to zero.
Pages: 12