Let me see if I understand what you are trying to do.
You want to display a menu.
On the menu, there are several ways to select an option.
1) The user could just type a letter 'a'..'g' or 'A'..'G'.
2) The user could use the up and down arrow keys to move the cursor to one of the lines and press Enter.
Also, I assume you are using an old Turbo C compiler. (Because if you are not, the following information will not help.)
You should be aware that the state of CS in India is 15-20 years out of date. If you have time, get yourself a modern compiler and play with it using modern C++ standards. Knowing how to use it will put you ahead.
The input should be aware of the location of the cursor relative to the top of the menu. It should also not permit the cursor to move outside of those bounds.
Personally, I would use
text to display the "cursor", and leave the actual blinking thing down in the corner.
I don't currently have an old copy of Turbo C installed, so I cannot test the following code for correctness. You may have to fix a thing or two to make it work.
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
|
void draw_main_menu()
{
// display the menu here
}
int get_main_menu_choice()
{
// This keeps track of the currently selected item
int current = 0;
// The cursor needs to be placed at the first item in the menu
gotoxy(13,16);
// Now we need to wait for the user to press something correct
while (true)
{
switch (getch())
{
// First we will handle the user pressing a letter
case 'a': case 'A': return 0;
case 'b': case 'B': return 1;
...
case 'g': case 'G': return 6;
// Next, we will handle arrow keys.
// When the user presses an arrow key, the first thing you will
// get from getch() is either 0 (on older hardware) or 0xE0 (on newer
// hardware). So if either of these was the key we got, then the next
// thing we get from getch() will be the "extended key code" for the
// arrow key (and other special keys).
case 0: case 0xE0:
switch (getch())
{
// up arrow key
case 72:
if (current > 0) // (don't go above the first item)
{
current -= 1;
gotoxy(current+13,16);
}
break;
// down arrow key
case 80:
if (current < 6) // (don't go below the 6th item)
{
current += 1;
gotoxy(current+13,16);
}
break;
// all other special keys -- ignored
default: break;
}
break;
// If the user presses Enter, choose the current menu option
case 13: return current;
// All other keys -- ignored
default: break;
}
}
}
void add()
{
clrscr();
cout << "If God wills it.\n";
gotoxy(1,20);
cout << "Press any key" << flush;
switch (getch()) { case 0: case 0xE0: getch(); }
}
int main()
{
bool done = false;
while (!done)
{
draw_main_menu();
switch (get_main_menu_choice())
{
case 0: add(); break;
case 1: display_all(); break;
case 2: display_individual(); break;
...
case 6: done = true; break;
}
}
cout << "Good-bye!\n";
return 0;
}
|
That should work for you. Remember, I may have made a small mistake in there somewhere, but you should be able to figure it out.
Once you get that working, there are a couple more issues you should consider.
The first is that the draw menu, get menu choice, and main functions all must know something about what the menu looks like. Specifically, every function must know that the menu has seven items, and what those items are numbered. You can use an enumeration to help.
Wife just came home -- I've got to go now. (We're going out.)
[edit]
The second is hardcoding the location of the menu is problematic. You should be able to place the menu at a coordinate given by argument to the menu functions.
The last is that it might be worth making the menu itself a data-driven object. That is, create a
struct that represents the menu and its items. Then pass that "menu" struct to the menu functions.
Hope this helps.