C++ text based menu

Gday all

Can someone tell me if there is a better way to make this work?
It is on Dev-C++, for windows only.

It is working however I am sure there is a better way. The main problem with it is you have to press enter twice to select a menu option.

I want to make it as streamlined as possible because it is going in a larger program.
(Can I make this another file to refer to in the other file? As it stands i intend to just copy it all over to add to the main file)

Any assistance is much appreciated!


EDIT - New code
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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <stdio.h>
#include <string>

/*
W = 87/119
S = 115/83
A = 97/65
D = 100/68
*/

using namespace std;


void gotoxy(int x, int y);
void gotoxy(COORD coord);
void getmove();

class menu{
      public:
             menu(COORD a, COORD b, COORD c, string a1, string a2, string a3);
             void writemenu();
             void listenselect();
      protected:
                COORD menucoords[3];
                string menopt[3];
                bool quit;
                bool menufuctions(int choice);
             
};

menu::menu(COORD a, COORD b, COORD c, string a1, string a2, string a3){
                 menopt[0] = a1;
                 menopt[1] = a2;
                 menopt[2] = a3;
                 menucoords[0] = a;
                 menucoords[1] = b;
                 menucoords[2] = c;
                 quit = FALSE;
}


void menu::writemenu(){
     system("CLS");
     for (int n = 0; n < 3; n++){
         gotoxy(menucoords[n]);
         cout<<menopt[n];
         }
}

void menu::listenselect(){
     int coordref = 0;
     gotoxy(menucoords[0]);
     cout<< ">";
     int ch = getch();
     quit = FALSE;
     while (quit == FALSE){
     while (ch != 13){
            gotoxy(menucoords[coordref]);
            putchar(32);
            if ((ch==87)||(ch==119)){
                                   if (coordref > 0){coordref = coordref - 1;}
                                   }
            if ((ch==83)||(ch==115)){
                                   if (coordref < 2){coordref = coordref + 1;}
                                   }
            gotoxy(menucoords[coordref]);
            cout<< ">";
            ch = getch();
            }
     menufuctions(coordref);
     ch = 0;
     }
            
}

bool menu::menufuctions(int choice){
     gotoxy(5,20);
     cout << "Menu choice ref number "<< choice<< " selected.";
     if (choice == 2) {quit = TRUE;}
}

int main(){
    COORD a = {2, 4};
    COORD b = {2, 6};
    COORD c = {2, 8};
    COORD d = {6, 2};
    COORD e = {6, 4};
    COORD f = {6, 6};
    cout<<"Use W and S to navigate up and down, ENTER to select";
    getch();
    menu castle = menu(a, b, c, "  Option 1", "  Option 2", "  Exit");
    menu combat = menu(d, e, f, "  Menu 2 Option 1", "  Menu 2 Option 2", "  Menu 3 Exit");
    castle.writemenu();
    castle.listenselect();
    combat.writemenu();
    combat.listenselect();
}


void gotoxy(COORD coord)
{
	SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE),coord);
}



void gotoxy(int x,int y)
{
	COORD coord = {x-1,y-1};
	SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE),coord);
}

Last edited on
Is there a better way to do this?
Last edited on
Topic archived. No new replies allowed.