-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppd_menu.c
More file actions
95 lines (78 loc) · 2.12 KB
/
ppd_menu.c
File metadata and controls
95 lines (78 loc) · 2.12 KB
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
/***********************************************************************
* COSC1076 - Advanced Programming Techniques
* Semester 2 2016 Assignment #2
* Full Name : Drew Nuttall-Smith
* Student Number : s3545039
* Course Code : COSC1076
* Program Code : BP096
* Start up code provided by Paul Miller
* Some codes are adopted here with permission by an anonymous author
***********************************************************************/
#include "ppd_menu.h"
/**
* @file ppd_menu.c handles the initialised and management of the menu
* array
**/
/**
* Initialises the menu array with names and function pointers
**/
void init_menu( struct menu_item* menu) {
/* Variables */
int i;
char menu_names[NUM_MENU_ITEMS][MENU_NAME_LEN + 1] = {
"Display Items",
"Purchase Items",
"Save and Exit",
"Add Item",
"Remove Item",
"Display Coins",
"Reset Stock",
"Reset Coins",
"Abort Program"
};
menu_function menu_functions[NUM_MENU_ITEMS] = {
&display_items,
&purchase_item,
&save_system,
&add_item,
&remove_item,
&display_coins,
&reset_stock,
&reset_coins,
NULL
};
for (i = 0; i < NUM_MENU_ITEMS; i++) {
strcpy(menu[i].name, menu_names[i]);
menu[i].function = menu_functions[i];
}
}
/**
* Prints the menu and prompts user to select an option
**/
void print_menu(struct menu_item* menu) {
int i;
printf("\nMain Menu:\n");
for (i = 0; i < NUM_MENU_ITEMS; i++) {
if (i == 3) printf("Administrator-Only Menu:\n");
printf("%d.%s\n",(i + 1),menu[i].name);
}
printf("Select your option (1-9): ");
}
/**
* Get the user's input and return the appropriate function pointer
**/
menu_function get_menu_choice(struct menu_item * menu) {
/* Variables */
char buffer[1 + EXTRACHARS];
int bufferLength = 1 + EXTRACHARS;
char *ptr = NULL;
int selection;
/* Get user input from stdin and check the buffer */
fgets(buffer, bufferLength, stdin);
if (buffer[0] == '\n') return NULL;
if (checkBuffer(buffer, bufferLength) == FALSE) return NULL;
/* Read value from buffer as number */
selection = strtol(buffer, &ptr, 10);
printf("\n");
return menu[selection - 1].function;
}