menu using getch() (Borland C)

hello ! need some help guys

I am a newbie and i got a project for school to create a menu using getch() but i dont know how to read the arrow keys, up and down key, i know their ASCII code 72 , 80, and 13 for enter.Can somebody help me with the code!
for example to show some like this:
1.Option 1
2.Option 2
3.Option 3

and when i press up arrow the upper line to be highllighted.Also using swith and case.

Comments

  • [code]char ch;

    do
    {
    ch = getch();

    switch(ch)
    {
    case 13:
    gotoxy(x, y);
    printf(">"); /* some sort of marker */
    break;

    case ...
    }
    } while (ch != 27); /* escape */
    [/code]
  • yeah thanx it worked. Sadly i have just found out i am not allowed to use gotoxy() for this project:(

    I asked the teacher and he said the soft should be like this.
    Put a counter that counts the number of the line, and the text should be vectors

    strcpy (menu[o], "1.Option 1");
    strcpy (menu[1], "2.Option 2");
    etc ..

    At start first option should be white text with blue background, hit the down arrow (which u get it with getch) the counter increments and it highlights the second line.
    Any ideas...


  • Then you have to clear the screen all the time. The program output will be glitchy and ugly.

    Why can't you use gotoxy() while you can use getch()? It doesn't make sense: both are non-standard functions from the same library. They are equally poor/handy to use.
  • I dont understand many things about my teacher too, i am in the 1st year in University and we just started C couple of months ago.Now each of us has a project that will be our final grade this year.I have to do this to pass. These are my teacher`s conditions.I have to do this without using gotoxy(), the teacher said its too easy if I use it.

    Isnt it possible smth like this:
    [code]
    if(ch=getch())
    {
    switch(ch)
    {
    case 80:printf("%s",menu[i]);
    i++ // 80 is ASCII for down key and i to be a contor
    }

    }
    [/code]
    This is just a draft maybe i even made some mistakes but i wish to know if its possible,or how else could i do the menu


  • Tell your teacher that your options are these:

    - Write the code in pure ANSI/ISO C. You will then have to print out the menu each time and scroll the console down. There is no other way to do it in standard C.

    - Clear the screen before displaying the menu again. This will be glitchy and ugly. It is not possible to do in pure standard C, the closest you get is system("cls"), but then the code will only work in Windows. clrscr() is another option, but it isn't standard C.

    - Use gotoxy() or API functions.


    In case your teacher happen to be one of those crappy programming teachers, then ignore him and focus on learning the standard C. How to do console stuff with getch() and gotoxy() in DOS, an OS that went obsolete 13 years ago, is useless knowledge really.
  • [color=Blue]..hmm... he can't just ignore the teacher. And, most likely, if Turbo C is used, then it is OK to use its library. You have to clear screen each keypress and draw the whole thing. I'll post some code later. I hope it is OK to use any functions setting the text color? Can you list the prohibited functions? What else (beside gotoxy()) cannot be used?[/color]
  • [color=Blue]Here is a menu (I got carried away, probably...):[/color]
    [code]
    #include

    [color=Green]// -------------------------------------------------------------------------[/color]
    char* menu [] =
    {
    " 1. FIRE PHASERS ",
    " 2. FIRE PHOTON TORPEDOES ",
    " 3. RAISE SHIEDLS ",
    " 4. BEGIN SELF-DESTRUCT SEQUENCE ",
    " 5. NEW GAME ",
    " 6. EXIT "
    };

    [color=Green]// -------------------------------------------------------------------------[/color]
    int nSelectedIndex = 0;
    int bRunning = 1;
    int nMenuItemCount = sizeof (menu) / sizeof (menu [0]);

    [color=Green]// -------------------------------------------------------------------------[/color]
    void DrawMenu ()
    {
    int iMenuItem;

    textbackground (BLACK);
    clrscr ();

    textcolor (YELLOW);
    cprintf ("
    *** MAIN MENU ***

    ");

    for (iMenuItem = 0; iMenuItem < nMenuItemCount; iMenuItem++)
    {
    if (iMenuItem == nSelectedIndex)
    {
    [color=Green]// We drawing the selected item
    //[/color]
    textcolor (WHITE);
    textbackground (BLUE);
    }
    else
    {
    [color=Green]// We drawing the un-selected item
    //[/color]
    textcolor (LIGHTGRAY);
    textbackground (BLACK);
    }

    cprintf (menu [iMenuItem]);
    cprintf ("
    ");
    }
    }

    [color=Green]// -------------------------------------------------------------------------[/color]
    void ExecuteCmd ()
    {
    switch (nSelectedIndex)
    {
    case 0:
    [color=Green]// PHASERS[/color]
    break;

    case 1:
    [color=Green]// PHOTONS[/color]
    break;

    case 2:
    [color=Green]// SHIELDS[/color]
    break;

    case 3:
    [color=Green]// SELF-DESTRUCT
    // (STARFLEET WILL NOT BE HAPPY ABOUT THAT!!)[/color]
    break;

    case 4:
    [color=Green]// DUMP THIS GAME AND START A NEW ONE![/color]
    break;

    case 5:
    bRunning = 0;
    break;
    }
    }

    [color=Green]// -------------------------------------------------------------------------
    // Return 1 if selection index changed
    // -------------------------------------------------------------------------[/color]
    int ExtendedKeyPress (int key)
    {
    int iRetValue = 0;

    switch (key)
    {
    case 72: [color=Green]// UP ARROW[/color]
    if (nSelectedIndex > 0)
    {
    nSelectedIndex--;
    iRetValue = 1;
    }
    break;

    case 80: [color=Green]// DOWN ARROW[/color]
    if (nSelectedIndex < (nMenuItemCount-1))
    {
    nSelectedIndex++;
    iRetValue = 1;
    }
    break;
    }
    return iRetValue;
    }

    [color=Green]// -------------------------------------------------------------------------[/color]
    int main ()
    {
    int nKey;
    int bRefreshMenu = 1;

    while (bRunning == 1)
    {
    [color=Green]// Do not redraw menu each key press, only
    // redraw if menu selection index was changed
    //[/color]
    if (bRefreshMenu == 1)
    {
    DrawMenu ();
    }

    nKey = getch ();
    switch (nKey)
    {
    case 0:
    [color=Green]// Zero returned by 'getch()' means that this key is an extended
    // key. In this case the next code returned by getch() is the one
    // we need to identify the key. For example: pressing DOWN ARROW
    // will generate code 0 plus code 80.
    //[/color]
    bRefreshMenu = ExtendedKeyPress (getch ());
    break;

    case 13: [color=Green]// ENTER KEY[/color]
    ExecuteCmd ();
    break;
    }
    }

    textbackground (BLACK);
    clrscr ();

    return 0;
    }
    [/code]
  • : [color=Blue]..hmm... he can't just ignore the teacher. [/color]

    Depends on if the goal is to learn something or to pass the course, or both.
  • yes this is what i need, but i dont have the experience to write such a code,last week we just finished functions. Thanks for ur help, i think i understand the code. Of course i want to learn C but i cant neglect university.
  • int opcion=1;//option
    char tecla;//key(Im from central america,the variables names are on spanish

    while(true){
    while(true){
    system("cls");

    if (opcion==1)
    cout<<"
    1-OPTION1<--";
    else
    cout<<"
    1-option1";

    if(opcion==2)
    cout<<"
    2-OPTION2<--";
    else
    cout<<"
    2-option2";

    if(opcion==3)
    cout<<"
    3-OPTION3<--";
    else
    cout<<"
    3-option3";

    if(opcion==4)
    cout<<"
    4-EXIT<--";
    else
    cout<<"
    4-exit";


    tecla=getch();
    if (tecla==13)
    break;

    if (tecla==-32){
    tecla=getch();
    if((tecla==72)&&(opcion>1)){//up
    opcion--;
    }
    if((tecla==80)&&(opcion<4)){//down
    opcion++;
    }
    }
    }
    if (opcion==1)
    //option1 code goes here
    cout<<'
    '<<'1'<<'
    ';

    if (opcion==2)
    //option2 code goes here
    cout<<'
    '<<'2'<<'
    ';
    if (opcion==3)
    //option3 code goes here
    cout<<'
    '<<'3'<<'
    ';
    if(opcion==4)
    //exit
    cout<<'
    '<<"exit"<<'
    ';


    system("pause");
    }

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories