c++ - 查看我的 C++ 滚动菜单并提供任何提示

标签 c++ visual-studio-2010 visual-studio visual-studio-2012 menu

现在我有一个可用的滚动菜单,我只是想问一下是否有人可以提供如何压缩它的提示,因为它有点“笨拙”!
我还有两个额外的问题:如何为我的
更改它 “摇滚,
纸,
剪刀”
像“剪刀石头布”一样显示?如果你试试我的代码,你会看到文本都是垂直的,我可以把它全部水平吗?有没有什么技巧可以让它变成一个无效的声明多次使用?

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

using namespace std;

int main()
{
string Menu[3] = {"Rock", "Paper", "Scissors"};
int pointer = 0;

while(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 -= 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:
                {
                    cout << "\n\n\nStarting new game...";
                    Sleep(1000);
                } break;
                case 1:
                {
                    cout << "\n\n\nThis is the options...";
                    Sleep(1000);
                } break;
                case 2:
                {
                    return 0;
                } break;
            }
            break;
        }
    }

    Sleep(150);
}

return 0;
}

最佳答案

有很多不同的方法可以解决这个问题。我更愿意在我自己的代码中以不同的方式来处理它,但是在一天中的这个时候我已经尝试编写一些尽可能接近你自己的代码的东西。

您可以为任何需要显示的菜单重复使用相同的函数 - 只需为字符串列表和该数组中的字符串数量的计数赋予不同的值。更好的是 - 使用 std::vector<string> .

我没有在主函数中硬编码菜单项的数量,而是通过将字符串数组的总大小除以第一个元素的大小来计算。由于无论字符串的长度如何,每个元素都为 sizeof 提供相同的值,因此您可以计算数组中的元素数。但请注意 - 这个技巧在 getUserChoice 中不起作用虽然函数 - 到那时,数组已经降级为指针,并且函数无法告诉数组中有多少项,这与 main 不同 - 这就是我计算那里数字的原因。使用字符串 vector 会更简洁,因为您只需将 vector 的引用传递给函数,然后在函数内部您可以调用 .size() vector 的方法来获取它包含的项目数。可能有点超出您刚才的位置,因此代码与您自己的风格相似。

希望对您有所帮助。 :)

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

using namespace std;

int getUserChoice(string *items, int numItems)
{
    int curSel = 0, textAttrib;
    bool selectionMade = false, needsUpdate = true; // we want to clearscreen first time through
    while (!selectionMade)
    {
        // only redraw the screen if something has changed, or we're on the first iteration of
        // our while loop.
        if (needsUpdate)
        {
            system("cls");
            needsUpdate = false;

            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Main Menu" << endl;
            cout << "(navigate with arrow keys, select with enter)" << endl << endl;
            for (int i=0; i<numItems; i++)
            {
                if (i == curSel)
                    textAttrib = 11;
                else
                    textAttrib = 15;

                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), textAttrib);

                if (i)
                    cout << " ";

                cout << items[i];
            }
        }

        if (GetAsyncKeyState(VK_LEFT) != 0)
        {
             curSel--;
             needsUpdate = true;
        }

        else if (GetAsyncKeyState(VK_RIGHT) != 0)
        {
             curSel++;
             needsUpdate = true;
        }

        else if (GetAsyncKeyState(VK_RETURN) != 0)
            selectionMade = true;

        if (curSel < 0)
            curSel = numItems-1;
        else if (curSel > (numItems-1) )
            curSel = 0;
        Sleep(100);
    }
    char dummy[10];
    cin.getline(dummy, 10);   // clear the enter key from the kbd buffer
    return curSel;
}


int main()
{
    string Menu[] = {"Rock", "Paper", "Scissors"};

    int selection;
    selection = getUserChoice(Menu, sizeof(Menu)/sizeof(Menu[0]) );

    cout << "You chose: " << Menu[selection] << endl;
}

关于c++ - 查看我的 C++ 滚动菜单并提供任何提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317093/

相关文章:

visual-studio - SSDT 模式比较 - 忽略系统服务代理(VS 2013)

c++ - 使用 concurrent_unordered_map 时崩溃

visual-studio - 遇到异常。这可能是由 Visual Studio 2013 中的扩展引起的

c# - 如何让 Visual Studio 2015 xproject (project.json) 引用依赖项目的最高框架

c++ - operator+() 选择右值引用变体而不是 const 左值变体

c++ - 我如何拍摄 stdafx.h?

c# - SqlCe 参数错误

c++ - 如何比较 boost::variant 以使其成为 std::map 的键?

c++ - 基本 C++(我认为是 cout 缓冲区还是溢出?!)

c++ - malloc() 非确定性行为