提问者:小点点

有没有一种方法可以在建立的for循环框中创建一个菜单(C++)


我对学习C++还很陌生,不知道如何在我用ASCII创建的另一个for循环框中添加一个“for循环菜单”。

我试图创建一个菜单与7个选项使用一个数组内部已经构建的框。 在我有限的知识中,我认为我需要用数组再添加一个for循环,因为我已经知道了选项的数量? 我试图在for循环中使用for循环是不是错了?

对于我应该将for循环菜单放在哪里有什么建议吗? 或者有没有更高效的方式将菜单添加到框中?

下面是我正在为该框打印的代码的副本:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    // This is a loop structure for the menu box using ASCII
    // This prints the top left corner of the menu box (ASCII)

    for (int row = 0; row < 1; ++row) {
        cout << char(201);
    }

    // This prints the top border of the menu box (ASCII)

    for (int column = 0; column < 80; ++column) {
        cout << char(205);
    }

    // This prints the top right corner of the menu box (ASCII)

    for (int row = 0; row < 1; ++row) {
        cout << char(187);
    }

    cout << "\n";

    // This prints the left border of the menu box (ASCII)

    for (int row = 0; row < 20; ++row) {
        cout << char(186);

        // This prints spaces inside the menu box

        for (int column = 0; column < 80; ++column) {
            cout << " ";
        }

        // This prints the right border of the menu box (ASCII)
        cout << char(186) << "\n";
    }

    // This will print the bottom left corner of the menu box (ASCII)

    for (int row = 0; row < 1; ++row) {
        cout << char(200);
    }

    // This prints the bottom border of the menu box (ASCII)

    for (int column = 0; column < 80; ++column) {
        cout << char(205);
    }

    // This prints the bottom right corner of the menu box (ASCII)

    for (int row = 0; row < 1; ++row) {
        cout << char(188);
    }

    cout << "\n";

    system("pause");

    return 0;
}

共1个答案

匿名用户

使用您选择的方法,您需要添加带有菜单选项的数组并打印它们,而不是在菜单框内打印空格。 要用菜单选项保存行对齐,可以使用中的setw()。

结果代码如下所示:

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
    const int menuWidth = 80;

    // This is a loop structure for the menu box using ASCII
    // This prints the top left corner of the menu box (ASCII)
    cout << char(201);

    // This prints the top border of the menu box (ASCII)
    for (int column = 0; column < menuWidth; ++column) {
        cout << char(205);
    }

    // This prints the top right corner of the menu box (ASCII)
    cout << char(187);
    cout << "\n";

    // array with menu options
    const string menuOptions[] = { "Option1", "Option2", "Option3", "Option4", "Option5", "Option6", "Option7" };

    for (string option : menuOptions)
    {
        cout << char(186) // print left border
            << setw(menuWidth) // set next item width
            << left // set next item aligment
            << option // print menu option string with width and aligment
            << char(186) << "\n"; // print right border
    }


    // This will print the bottom left corner of the menu box (ASCII)
    cout << char(200);

    // This prints the bottom border of the menu box (ASCII)
    for (int column = 0; column < menuWidth; ++column) {
        cout << char(205);
    }

    // This prints the bottom right corner of the menu box (ASCII)
    cout << char(188);
    cout << "\n";

    system("pause");

    return 0;
}

此外,您还可以使用缩进,或者在菜单的顶部或底部添加用于偏移量的空行,以使其看起来更好。

和一些关于代码的说明:

  • 您不需要使用for循环来打印一个符号(如拐角)C:
  • 常量值,您可以多次使用这些值(如菜单的宽度),最好保存在常量中

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(一种|方法|建立|循环|框|中|创建|菜单|c++)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?