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!!
/*
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>
usingnamespace 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)
{
casetrue:
cout << option_text << "\t\tO" << endl;
break;
casefalse:
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;
}
elseif(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;
}
}
}
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...)