Hello daniweb, I've been trying to impliment arrow keys into my program, I've found a way, but the problem is I cant figure out how to use the enter key to select an option as follows:

pawns:
    int pawn;
    system("cls");
    pawndd:
    gotoxy(0,0);
       SetConsoleTextAttribute(hColor,455);
    cout<<"  RETURN TO PREVIOUS MENU";
    SetConsoleTextAttribute(hColor,23);
    cout<<"\n";
    cout<<"  INTEREST CALCULATOR";
    cout<<"\n";
    cout<<"  RENEW, EXTEND, OR REDEEM";
    //cin>>pawn;

kbmain=_getch();



      /*if(kbmain!='\0')
        {
            kbmain=_getch();
        }
        */


        kbmain=getch();



    if(kbmain == 'H')
    {
        goto hrer;
    }

    if(kbmain == 'P')
    {
        hintcalc:
    gotoxy(0,0);
    cout<<"  RETURN TO PREVIOUS MENU";
    cout<<"\n";
    SetConsoleTextAttribute(hColor,455);
    cout<<"  INTEREST CALCULATOR";
    SetConsoleTextAttribute(hColor,23);
    cout<<"\n  RENEW, EXTEND, OR REDEEM";
    cout<<"\n";

    }

    kbmain2=_getch();

      if(kbmain2!='\0')
        {
            kbmain2=_getch();
        }
         if(kbmain2 == 'H')
    {
        goto pawndd;
    }
        if(kbmain2=='P')
        {
            hrer:
                gotoxy(0,0);
    cout<<"  RETURN TO PREVIOUS MENU";
    cout<<"\n";
    SetConsoleTextAttribute(hColor,23);
    cout<<"  INTEREST CALCULATOR";
    SetConsoleTextAttribute(hColor,455);
    cout<<"\n";
    gotoxy(0,2);
    cout<<"  RENEW, EXTEND, OR REDEEM";
    SetConsoleTextAttribute(hColor,23);


        }
            kbmain3=_getch();

      if(kbmain3!='\0')
        {
            kbmain3=_getch();
        }
         if(kbmain3=='H')
        {
            goto hintcalc;
        }
         if(kbmain3=='P')
        {
            goto pawndd;
        }





    if(pawn == 3)
    {
        string p1;
        string p2;
        string p3;
        string p4;
        string p5;
        string p6;
        string p7;
        string p8;
        string p9;
        string p10;
        string p11;
        string p12;
        string p13;
        string p14;
        string p15;
        string p16;
        string p17;
        string p18;
        float extend;
        float renew;
        float redeem;
        char getloan[6];
        string err;
        system("cls");
        cout<<"Enter The Pawn # To Open: ";
        cin>>getloan;
        ifstream pay (getloan);
        {
            getline(pay, p1);
            getline(pay, p2);
            getline(pay, p3);
            getline(pay, p4);
            getline(pay, p5);
            getline(pay, p6);
            getline(pay, p7);
            getline(pay, p8);
            getline(pay, p9);
            getline(pay, p10);
            getline(pay, p11);
            getline(pay, p12);
            getline(pay, p13);
            getline(pay, p14);
            getline(pay, p15);
            getline(pay, p16);
            getline(pay, p17);
            getline(pay, p18);
        }
        cout<<p1<<p2<<p3<<endl;
        cout<<p4<<endl;
        cout<<p5<<endl;
        cout<<p6<<endl;
        cout<<p7<<endl;
        cout<<p8<<endl;
        cout<<p8<<endl;
        cout<<p9<<endl;
        cout<<p10<<endl;
        cout<<p11<<endl;
        cout<<p12<<endl;
        cout<<p13<<endl;
        cout<<p14<<endl;
        cout<<p15<<endl;
        cout<<p16<<endl;
        cout<<p17<<endl;
        cout<<p18<<endl;

cout<<"1.Extend     2.Renew     3.Redeem";
cin>>err;
if(err == "1");
{
    cout<<"Extension Amount: $15.00";
    cout<<"\n\nAmount To Collect NOW ->";
    cin>>extend;
    cout<<"Processing. . . ";
    Sleep(250);
    goto extras;
}


        system("cls");
        goto extras;
    }
    if(pawn == 2)
    {
        intcalc:
        float borrowed;
        int apr;
        int years;
        float interest;
        system("cls");
        cout<<"ENTER AMOUNT BORROWED $";
        cin>>borrowed;
        if(borrowed > 499.99)
        {
            pwd_ent:
            system("cls");
            int temp_c=rand()%99999;
            int temp_k;
            int temp_p;
            cout<<"ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"<<endl;
            cout<<"ÛÛ       Û  Password Required  Û     ÛÛ"<<endl;
            cout<<"ÛÛ       ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ     ÛÛ"<<endl;
            cout<<"ÛÛ                                   ÛÛ"<<endl;
            cout<<"ÛÛ                                   ÛÛ"<<endl;
            cout<<"ÛÛ Loan Between $500.00 and $1240.00 ÛÛ"<<endl;
            cout<<"ÛÛ                                   ÛÛ"<<endl;
            cout<<"ÛÛ                                   ÛÛ"<<endl;
            cout<<"ÛÛ  ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ ÛÛ"<<endl;
            cout<<"ÛÛ  ÛÛ Temporary Code "<<temp_c<<"       ÛÛ ÛÛ"<<endl;
            cout<<"ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ"<<endl;
            temp_k = temp_c + 22222;
            cout<<"Enter Password. . . ";
            cin>>temp_p;
            if(temp_p == temp_k)
            {
                goto ap;
            }
            else {
                system("cls");
                goto pwd_ent;
            }

        }
        else{
            goto ap;
        }
        ap:
        cout<<"ENTER APR %: ";
        cin>>apr;
        apr = apr / 100;
        cout<<"ENTER TIME IN YEARS: ";
        cin>>years;
        interest = borrowed * apr * years;
        cout<<"INTEREST = "<<interest<<endl;
        system("pause");
        goto pawns;
    }
    if(pawn == 1);
    {
        system("cls");
     goto extras;

    }

}
}

Please help me find a way to use enter to select an option.

Recommended Answers

All 3 Replies

Rather than try to decipher and work with that mess, I'll give you an example of what I think you're asking for:

#include <iostream>
#include <string>
#include <Windows.h>
#include "coniolib.h"

namespace {
    win32::ConioLib conio;

    enum {
        KEY_ENTER = 13,
        KEY_ESC   = 27,
        KEY_UP    = 256 + 72,
        KEY_DOWN  = 256 + 80,
        KEY_LEFT  = 256 + 75,
        KEY_RIGHT = 256 + 77
    };

    enum {
        HILITE_SELECTED   = 433,
        HILITE_UNSELECTED = 23
    };

    int get_key(void)
    {
        int ch = conio.getch();

        if (ch == 0 || ch == 224) {
            ch = 256 + conio.getch();
        }

        return ch;
    }
}

int menu(int selected_row = 1)
{
    const int size = 3;
    const std::string rows[size] = {
        "1) Option 1",
        "2) Option 2",
        "3) Option 3"
    };

    conio.clrscr();

    if (selected_row < 1) {
        selected_row = 1;
    }

    if (selected_row > size) {
        selected_row = size;
    }

    for (int i = 0; i < size; i++) {
        if (i + 1 == selected_row) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), HILITE_SELECTED);
        }

        std::cout << rows[i] << '\n';

        if (i + 1 == selected_row) {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), HILITE_UNSELECTED);
        }
    }

    std::cout.flush();

    return selected_row;
}

void execute_selection(int selection)
{
    std::cout << "You selected option " << selection << '\n';
}

int main()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), HILITE_UNSELECTED);

    int selected_row = menu(1);
    bool done = false;

    while (!done) {
        Sleep(100);

        if (conio.kbhit()) {
            switch (get_key()) {
            case KEY_UP:
                selected_row = selected_row > 1 ? selected_row - 1 : selected_row;
                menu(selected_row);
                break;
            case KEY_DOWN:
                selected_row = selected_row < 3 ? selected_row + 1 : selected_row;
                menu(selected_row);
                break;
            case KEY_ENTER:
                execute_selection(selected_row);
                break;
            case KEY_ESC:
                done = true;
                break;
            default:
                break; // Ignore unsupported keys
            }
        }
    }
}

Note that my compiler doesn't fully support the conio.h library, so I'm using a self-written class that implements the key features. I'll probably clean up that library and post it as a code snippet eventually.

That's Not exactly what im looking for. What you've done is creating a menu driven by arrow keys but there is no way to assign a function to a specific option.

but there is no way to assign a function to a specific option.

That's not true. I simply chose not to complicate the example by making each selection its own function since it appeared that your issue was menu item selection, not menu item execution.

As a further example, consider that if you have the row of the item you want to execute, and an array of function pointers where each index matches the row you want, you can modify execute_selection() like this (everything else remains the same):

void add() { std::cout << "add()" << std::endl; }
void remove() { std::cout << "remove()" << std::endl; }
void display() { std::cout << "display()" << std::endl; }

typedef void (*action_t)();

action_t selection_actions[] = {
    nullptr,
    &add,
    &remove,
    &display
};

void execute_selection(int selection)
{
    selection_actions[selection]();
}

That's a quick and dirty example to show that it's possible to use the selected row as a key of some sort for calling a function. With a little effort you can no doubt come up with something better suited to your specific needs. Note that my example wasn't meant to be a complete solution to your problem, just something to get you started in the right direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.