首页 > 解决方案 > C中循环控制台菜单实现的问题

问题描述

我正在寻找一种实现控制台菜单的方法,然后我找到 了 LXSoft 关于“如何在 ANSI/ISO C 中编写控制台菜单?”的答案。 所以我尝试使用 Visual Studio 2017 在我的代码中实现它

编码:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <windows.h>
// LXSoft
// mod: cui/menu_021
// stdarg.h  -> used for variable list of arguments (va_list, va_start ...)
// windows.h -> used for Sleep function, for *nix use unistd.h

typedef unsigned short int usint_t;
// Menu function prototype
int menu(char* name, char* prefix, char* cursor, usint_t orientation,
    usint_t padding, usint_t start_pos, usint_t delay,
    usint_t num_childs, ...);

int main()
{
    int exit;
    do {
        exit = 1;
    int response = menu("List des commandes", "-", "-->", 1, 3, 1, 0, 2,
        "1- Ajouter un etudiant (ou un ensemble d'etudiants)",
        "2- exit");
    printf("\n");
    switch (response)
    {
    case 1:
        // doSomethingFoo1();
        exit = 0;
        break;
    case 2:
        //doSomethingFoo2();
        exit = 1;
        break;
    default:
        exit = 0;
    }
        printf("\nYour choice is: %d", response);
    } while (!exit);
    return 0;
}

// Menu implementation
int menu
(
    char *name,        // Menu name (eg.: OPTIONS)
    char *prefix,      // Menu prefix (eg.: [*])
    char *cursor,      // Menu cursor (eg.: ->)
    usint_t orient,    /*
                        * Menu orientation vertical or horzontal.
                        * 0 or false for horizontal
                        * 1 or true for vertical
                        */
    usint_t padding,   // Menu childrens padding (eg.: 3)
    usint_t start_pos, // Menu set active child (eg.: 1)
    usint_t delay,     // Menu children switch delay
    usint_t childs,    // Number of childrens
    ...                /*
                        * Variable list of arguments char* type.
                        * Name of the childrens.
                        */
)
{
    va_list args;
    int tmp = 0, pos;
    char chr=0;
    usint_t opt = start_pos;
    char* format = malloc
    (
        (
            strlen(name) + strlen(prefix) + strlen(cursor) +
            3 + /* menu suffix (1 byte) and backspace (2 bytes) */
            (2 * childs) + /* newline (2 bytes) times childs */
            (padding*childs) + /* number of spaces times childs */
            childs * 60 /* children name maxlen (15 bytes) times childs*/
            ) * sizeof(char)
    );
    do
    {

        if (tmp != 0)chr = _getch();
        if (chr == 0x48 || chr == 0x4B)
            (opt > 1 && opt != 1) ? opt-- : (opt = childs);
        else if (chr == 0x50 || chr == 0x4D)
            (opt >= 1 && opt != childs) ? opt++ : (opt = 1);
        else {/* do nothing at this time*/ }
        strcpy(format, "");
        strcat(format, prefix);
        strcat(format, name);
        strcat(format, ":");
        va_start(args, childs);
        for (tmp = 1; tmp <= childs; tmp++)
        {
            (orient) ? strcat_s(format, SizeFormat, "\n") : 0;
            pos = padding;
            while ((pos--) > 0) strcat_s(format, SizeFormat, " ");
            if (tmp == opt)
            {
                strcat(format, "\b");
                strcat(format, cursor);
            }
            strcat(format, va_arg(args, char*));
        }
        /*if(tmp!=childs)
        {
            fprintf(stderr,"%s: recieved NULL pointer argument,"
                           " child not named", __func__);
            return -1;
        }*/
        Sleep(delay);
        system("cls");
        fputs(format, stdout);
        va_end(args);
    } while ((chr = _getch()) != 0x0D);
    return opt;
}

它工作正常,但似乎我不能多次调用函数“菜单”(case1:),因为我试图循环它但它会中断并变得无法使用,换句话说我不能再使用光标了。(代码已编辑以更简单)

我在做一些愚蠢和错误的事情吗?

还是不可能循环?如果没有怎么办?

提前致谢

编辑: 我发现在第一次循环迭代后单击箭头键时使用的调试器没有if (tmp != 0)chr = _getch(); 给出正确的输入值。 这是为什么 ?

标签: cvisual-studio-2017windows-10

解决方案


这是我看到的简化形式:

int main()
{
    int exit;

    do {
        exit = 1;
        int response = menu(...);

        switch (response)
        {
        case 1:
        case 2:
            break;
        default:
            exit = 0;
        }

    } while (!exit);

    return 0;
}

只要menu()回报12exit == 1所以(!exit) == 0。这会缩短do...while循环。

我会推荐

exit = 0;       // exit is meaningful, this means "exit - false"
...
default:
    exit = 1;   // "exit - true"
...
while (!exit);  // while not exiting, loop

或者

keep_looping = 1;      // you keep the original value '1' but the variable
                       // name is meaningful; i.e. "keep_looping - true"
...
default:
    keep_looping = 0;  // "keep_looping - false"
...
while (keep_looping);  // "keep looping while keep_looping"

推荐阅读