Confusions with structures

Pages: 12
Okay so this is the code right now,
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
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
//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 <stdlib.h>
#include "Structure.h"
#include "CustomerInformation.h"

using namespace std;

void Introduction();			 // Prototype for Introduction
void printCheck(CustomerInfo);	 // Prototype for PrintCheck
void showMenu();				 // Prototype for showMenu
int getData();					 // Prototype for getData;


int main() {
	int CustomerNumber; // Gives each customer a number 0-99
	char Y_N = 'y';			
	char Continue;


	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; // prompts user to input their order
			cin >> order;

			cout << "You chose " << MenuList[order].menuItem		// prints out what the user chose
				<< "......" << MenuList[order].menuPrice << endl;
			
			Customer[CustomerNumber].orderItems[Customer[CustomerNumber].counter] = order;
			
			Customer[CustomerNumber].counter++; // incriments counter
			cout << " Would you like to order again (enter 'y,Y' or 'n,N )" << endl; // prompts user to input if they want to order again
			cin >> Y_N;
			
		} while (Y_N == 'y' || Y_N == 'Y'); // while the customer wants to order 
		//cout <<"This is the counter for customer" << Customer[CustomerNumber].counter << endl << endl; // Debugging

		system("CLS"); // clears the screen
		printCheck(Customer[CustomerNumber]);// Prints the check
		

		//Owner can end program if no other customers are in diner
		cout << endl << endl << endl; // clears a few lines 
		cout << "New Customer Please input 'C'" << endl 
			<< "Owner: Enter O" << endl;
		cin >> Continue;								//prompts user to input c for customer or o for owner to continue or end code
		if (Continue == 'c' || Continue == 'C') {		// if user enters c clear the screen and continue to loop
			system("CLS");
		}
		else if (Continue == 'o' || Continue == 'O') {	// if user enters o end code
			break;
		}


	}


	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) {
	double receipt_total = 0; // receipt total
	const double TAX_VALUE = .08125; // tax constant
	double tax;				  // Value for tax to be multiplied to receipt_total
	double Total;			  // Total for order w/ tax
	cout << "Here is your check, Please come again!" << endl << endl;
	
	for (int i = 0; i < ThisOrder.counter; i++) {		// Prints out the order for customer
		cout << MenuList[ThisOrder.orderItems[i]].menuItem << setfill('.') << setw(10)
			<< MenuList[ThisOrder.orderItems[i]].menuPrice << endl;
	}
	for (int i = 0; i < ThisOrder.counter; i++) {		//tallys up the total price and prints 
		receipt_total += MenuList[ThisOrder.orderItems[i]].menuPrice;
	}
	cout <<"Total w/o Tax" <<setfill('*')<< setw(10) <<"$" << receipt_total << endl;
	tax = receipt_total * TAX_VALUE;
	Total = tax + receipt_total;
	cout << "Total w/  Tax" << setfill('*') << setw(10) << "$" << Total << endl;;


}

//int getData()
//

//}

// Menu
// David Santiago
// November 30, 2018
// This function will present a menu to the user and will explain to them, how to select a food item to order.

void showMenu() {
	cout << setfill(' ') << setw(20) << "DCC Diner"<< endl;
	// loop to output the item and price of the arrays
	for (int i = 0; i < 2; i++)
	{
		// outputing the menu item and menu price
		cout << i <<": " << MenuList[i].menuItem << "................." << MenuList[i].menuPrice << endl;
	}

	// outputting an instruction on how to select the food itme the user wants
	cout << "In order to order food, please enter in a number between 0 and 8. The numbers correspond with the food items going down, the first food item is 0 and so on: " << endl;
}






I am having trouble with the getData function. One of myt partners was supposed to do it but they did it kind of wrong so now im trying to redo it. I posted another forum post about it but im not getting alot of replies for it right now. What im having problems with is writing the information from one file into the array of strings. the counsol crashes when ever i run the program and ive found it to be the counter++ crashing it. but without the counter then i can write the information into the array. here is the function:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "Header.h"
using namespace std;


int getData() {
	int counter = 0;
	double x; // placeholder for prices
	string y;
	//const int MAX_ITEMS = 20;



	ifstream menuItemData; // Definition of menuItemData file containing menu items
	ifstream menuPriceData;// Definition of menuPriceData file containing menu items
	menuItemType MenuList[8]; //array for menu
	cout << fixed << showpoint << setprecision(2);

	menuItemData.open("MenuItem.dat");		// opens file with menu items 
	menuPriceData.open("MenuPrice.dat");	// opens file with menu prices

	while (!menuItemData.eof()) {
		getline(menuItemData, MenuList[counter].menuItem);
		counter++;
	}



	counter = 0; // sets counter back to 0;
		//cout << counter; // debugging 
	while (counter < MAX_ITEMS && menuPriceData >> x) { // reads information into array MenuList[].menuPrice
		MenuList[counter].menuPrice = x;

		//cout << MenuList[counter].menuPrice << endl; // debugging 

		counter++;
		//cout << x << endl;
	}
	
	

	system("pause");
	return counter;
}


sorry for the long post: its uses the same structure, the files arent really important to the function i just need to know why this crashes
while (!menuItemData.eof()) {
getline(menuItemData, MenuList[counter].menuItem);
counter++;

file has less than 9 items, right?

and, if there are only 3 items, what does this do?

while (counter < MAX_ITEMS && menuPriceData >> x) { // reads information into array MenuList[].menuPrice
MenuList[counter].menuPrice = x;

There isn't much code here, and it looks mostly correct so long as you don't have a blowout from counter being out of bounds or trying to access something invalid in the second loop. (seems most likely)
Last edited on
so what do i need to fix in order for this to work?
Something 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
int getData() {
	constexpr int MAX_ITEMS = 8;
	menuItemType MenuList[ MAX_ITEMS ];

	ifstream menuItemData( "MenuItem.dat" );
	std::string name;
	int counter = 0;
	while ( counter < MAX_ITEMS && getline( menuItemData, name ) ) {
		// Add to array only IF
		// * there is room in the array AND
		// * read was successful
		MenuList[counter].menuItem = name;
		++counter;
	}
	// the array has now 'counter' items

	ifstream menuPriceData( "MenuPrice.dat" );
	int prices = 0;
	double value {};
	while ( prices < counter && menuPriceData >> value ) {
		MenuList[prices].menuPrice = value;
		++prices;
	}
	return prices;
}
Last edited on
my counsel crashes everytime i run the program, even with this code, why does this keep happening.


edit: for some reason my desktop computer was not running the code properly. it was the cause of the console crashing i believe. i now switched to another computer and it seems to run fine now. Thank you all for the help
Last edited on
Topic archived. No new replies allowed.
Pages: 12