【问题标题】:return function main menu in cc中的返回函数主菜单
【发布时间】:2016-06-25 08:59:17
【问题描述】:

我这周开始编码,所以我对此很傻。我需要有关在我的脚本中返回 main 的帮助。例如,当我完成课程注册部分时,我无法返回菜单程序崩溃

代码:

#include <stdafx.h>
#include <stdio.h>

void eng();
void menu();
void huh();

int main()
{
    menu();

    return 0;
}

void menu()
{
    int menu1choice;

    printf("Menu\n");
    printf("\n");
    printf("1. Student Registration\n");
    printf("2. Show Students.\n");
    printf("Please enter number: ");
    scanf_s("%d", &menu1choice);
    switch (menu1choice)
    {
        case 1:
        {
            eng();
            break;
        }

    }
}

void eng()
{
    int a = 5;
    char name[30];

    printf("1.Student Number: ");
    scanf_s("%d", &a);
    //student number
    printf("2.Name: ");
    scanf_s("%s", &name);
    //student name
    getchar();
}

void huh()
{
    int a = 5;
    char name[30];

    printf("Your Student number: %d\n", a);
    printf("Your Name: %s\n", name);
    //result
    getchar();
}

请帮助我编写返回代码行,在此先感谢

【问题讨论】:

  • 您调用了 undefined behavior 以使用 printf("Your Name: %s\n", name); 中具有自动存储持续时间的未初始化变量的不确定值
  • scanf_s("%s", &amp;name); --> scanf_s("%29s", name, 30); (30 sizeof(name) )
  • 您不需要在case 中使用{},但您确实应该使用default 子句来处理所有其他情况。同时发布 scanf_s 的代码
  • @terencehill 他没有 scanf_s() 的源代码,它是 Microsoft 的 C 运行时库的一部分。与标准 C scanf() 的主要区别在于,无论何时使用“%s”,它都需要一个缓冲区大小。
  • 不推荐使用非原型函数声明器:void eng() -> void eng(void)int main(void)

标签: c menu return main


【解决方案1】:

这里有一些代码可以帮助你理解函数返回值的机制,在你的例子中返回到主函数。

至于一些建议,请阅读有关幻数的内容,尤其是它们不好的原因。

/*
 *  35917794_main.c
 */

#include <stdio.h>

#define     STU_REG         1
#define     STU_SHOW        2
#define     EXIT_SUCCESS    0

unsigned int show_menu(void);

unsigned int main
        (
        unsigned int    argc,
        unsigned char   *arg[]
        )
{
    unsigned int    menu1choice;
/*
 *  The next statements says run the function show_menu() and put the returned
 *  result in the variable menu1choice.
 */
    menu1choice = show_menu();
    switch(menu1choice)
        {
        case (STU_REG):
            {
            printf("\nGo do STUDENT REGISTRATION things...\n\n");
            break;
            }
        case STU_SHOW:
            {
            printf("\nGo do SHOW STUDENT things...\n\n");
            break;
            }
        default:
            {
            printf("\nGo do something for invalid option...\n\n");
            break;
            }
        }
    return(EXIT_SUCCESS);
}


unsigned int show_menu
        (
        void
        )
{
    unsigned int    ui_W0;
    printf("Menu\n\n");
    printf("1. Student Registration\n");
    printf("2. Show Students.\n");
    printf("Please enter number: ");
    scanf("%d", &ui_W0);
/*
 *  The next statements says run the function show_menu() has finished and returned
 *  returns the result in the variable ui_W0.
 */
    return(ui_W0);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    相关资源
    最近更新 更多