简体   繁体   中英

Graphics Menu in c++

I am working on a project and I want to make a Graphics Menu. Issue which I am facing is that after it shows any text written in the function I've put in switch. it goes back to main menu. I want to make a function which stays on new function and once it directs to new function, It has nothing to do with main menu anymore. Until this function is called again.

I want it to be a simple menu function which directs me to function. Nothing else.

your help would mean allot! Thanks in advance.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<iomanip>
#include<fstream>
#include<windows.h>

using namespace std;


int main() {

    system("cls");
    string Menu[3] = { "                Admin", "               Customer", "                Exit" };
    int pointer = 0;
    bool flag=true;

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

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << "Main Menu\n\n";

        for (int i = 0; i < 3; ++i)
        {
            if (i == pointer)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
                cout << Menu[i] << endl;
            }
            else
            {
                SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE), 15);
                cout << Menu[i] << endl;
            }
        }

        while (true)
        {
            if (GetAsyncKeyState(VK_UP) != 0)
            {
                pointer =pointer-1;
                if (pointer == -1)
                {
                    pointer = 2;
                }
                break;
            }
            else if (GetAsyncKeyState(VK_DOWN) != 0)
            {
                pointer += 1;
                if (pointer == 3)
                {
                    pointer = 0;
                }
                break;
            }
            else if (GetAsyncKeyState(VK_RETURN) != 0)
            {

                switch (pointer)
                {
                case 0:
                {
                    admin_login();
                    Sleep(500);
                    break;
                }
                case 1:
                {
                    customer_sign();
                    Sleep(500);
                    break;
                }
                case 2:
                {
                    thank_you();
                    Sleep(800);
                    break;
                }
                default:
                    {
                        cout<<"Invalid Input! ";
                    }

                }

            }
        }

        Sleep(150);
    }

    return 0;
    }

Your 'flag' boolean needs to be checked as the condition for both while loops, not just the first one. You also need to clear the console when you press enter on a menu selection.

Inside your switch case after detecting key press, in each case you need to set your flag boolean to false so you stop drawing the main menu, and instead start drawing the sub menus. I don't have access to your other functions so here is a solution emulating that behaviour in a simplified and minimal reproducible proof of concept:

#include<windows.h>
#include <iostream>

using namespace std;

int main() 
{
    system("cls");
    string Menu[3] = { "                Admin", "               Customer", "                Exit" };
    int pointer = 0;
    bool bMainMenu = true;

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

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
        cout << "Main Menu\n\n";

        for (int i = 0; i < 3; ++i)
        {
            if (i == pointer)
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
                cout << Menu[i] << endl;
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                cout << Menu[i] << endl;
            }
        }

        while (bMainMenu)
        {
            if (GetAsyncKeyState(VK_UP)&1)
            {
                pointer = pointer - 1;
                if (pointer == -1)
                {
                    pointer = 2;
                }
                break;
            }
            else if (GetAsyncKeyState(VK_DOWN)&1)
            {
                pointer += 1;
                if (pointer == 3)
                {
                    pointer = 0;
                }
                break;
            }
            else if (GetAsyncKeyState(VK_RETURN)&1)
            {
                switch (pointer)
                {
                case 0:
                {
                    //admin_login();
                    system("cls");
                    std::cout << "admin selected\n";
                    Sleep(1000);
                    bMainMenu = false;
                    break;
                }
                case 1:
                {
                    //customer_sign();
                    system("cls");
                    std::cout << "customer selected\n";
                    Sleep(1000);
                    bMainMenu = false;
                    break;
                }
                case 2:
                {
                    //thank_you();
                    system("cls");
                    std::cout << "thank you selected\n";
                    Sleep(1000);
                    bMainMenu = false;
                    break;
                }
                default:
                {
                    cout << "Invalid Input! ";
                }
                }
            }
        }
        Sleep(150);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM