Menu selecting on a console application

Greetings! Just as a side project, I thought i'd try to make a menu selector in a console application. I know it may not be the best idea or the most efficient, but just humor me. haha
This current code is what my friend and I thought would work, but it seriously bugs out! I don't really know how to describe it. Hopefully you guys can help! =] Thanks in advance!!

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
  Name: Console Menu
  Copyright: NA
  Author: MR
  Date: 27/02/11 17:22
  Description: Just a quick console program to use menus...
*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;


//sets up the menu class
class menu
{
    public:
        
        //constructor
        menu();
        
        //a sub-class to hand the different option of the one menu
        class option
        {
            public:
                //sets up the properties of the numerical address
                //and whether or not it is selected
                int address;
                bool is_selected;
                
                //will be the actual option
                string option_text;
                
                //option constructor.. a little more complex
                option(int addr)
                {
                    addr = address;
                    char num = addr;
                    stringstream nums;
                    nums << num;
                    string numss = nums.str();
    
                    option_text.append(numss);
    
                    //if the initial address = 0, then it's selected from the get-go
                    if(address == 0)
                    {
                        is_selected = true;
                    }
                    //if it isn't 0, it ain't selected!!
                    else
                    {
                        is_selected = false;
                    }
                }

                //diplays the string of the current option...
                void display()
                {    
    
                    switch (is_selected)
                    {
                        case true:
                            cout << option_text << "\t\tO" << endl;
                            break;
                        case false:
                            cout << option_text << endl;
                            break;
                    }
                }

                //not much to do yet...
                void selected()
                {
    
                }
                //destructor
                ~option() {};
        };
        
        //sets up an array of options! (only a max of 5 though!!!)
        menu::option::option *fields[5];
        //used in the looping displays.. this number is set by user during constructor
        int max_num;
        
        //sets the current selected option
        int current_address;
        //sets the old address
        int old_address;
        //to be used in the event loop to handle any changes in the addressing
        void get_new_address();
        
        void check_address();
        
        //display the displays
        void display();
        
        //destructor
        ~menu() {};
        
};

//constructor function....
menu::menu()
{
    int number;
    
    cout << "how many options? <5 --> ";
    cin >> number;
    
    number = max_num;
    
    //sets up the fields based on the user input
    for(int i = 0; i < number; i++)
    {
        fields[i] = new menu::option::option(i);
    }
    
}

//displays the menu!
void menu::display()
{
    cout << "\n\nWhich one would you like...\n";
    
    for(int i = 0; i < max_num; i++)
    {
        fields[i]->display();
    }
}

//gets the new address of the selected option
void menu::get_new_address()
{
    fields[old_address]->is_selected = false;
    fields[current_address]->is_selected = true;
}

//makes sure the menu address doesn't go outside of the array size!!
//thats a big no no!!
void menu::check_address()
{
    if(current_address > max_num)
    {
        current_address = 0;
    }
    else if(current_address < 0)
    {
        current_address = max_num;
    }
}



//deletes everything
void end_all(menu *menu)
{        
    for(int i = 0; i < menu->max_num; i++)
    {
        delete menu->fields[i];
    }
    delete menu;
}
    

int main()
{
    //same old event programming stuff...
    bool exit_program = false;
    char key_input;
    
    //creates a neat new menu object!
    menu *mymenu = new menu;
    
    //O_o it's displaying!!!
    mymenu->display();
        
    //creates an infinite loop (with a chance of payroll)
    while(exit_program == false)
    {
        //checks of the keyboard input
        key_input = getch();
        

            mymenu->fields[0]->display();
            mymenu->fields[1]->display();

        //all the keyboard possibilities..
        switch(key_input)
        {
            case 119: //w
                //moves the selector up a number
                mymenu->old_address = mymenu->current_address;
                mymenu->current_address++;
                    mymenu->check_address();
                        mymenu->get_new_address();
                break;
            case 115: //s
                //moves the selector down a number
                mymenu->old_address = mymenu->current_address;
                mymenu->current_address--;
                    mymenu->check_address();
                        mymenu->get_new_address();
                break;
            case 106: //j
                //selects the option
                mymenu->fields[mymenu->current_address]->selected();
                break;
            case 112: //p
                //ends all and frees all the space
                end_all(mymenu);
                return 1;
                break;
            //otherwise
            default:
                //break out
                break;
        }
                
    }
}

I don't really know how to describe it.


Try.

What do you expect your program to do?
What does it actually do?
Last edited on
This program was to provide event selecting of options rather than text selecting..
i.e. - net hack

It works fine up until the event loop where it seemingly doesn't read the option.display function. Whenever I pressed 'w' 2 times, its gives 3 short beeps and starts spewing all kinds of information at me. They appear to be all the special characters using the alt + #numpad#.. Then it starts displaying the jumbled directories and it's just chaos. It comes up with an error message right after it finishes writing on the console.

If the logic is sound, then no problemo! I'll just apply this to an SDL program instead of messing with the "conio.h" header. (I've never used it before...)
Topic archived. No new replies allowed.