gpt4 book ai didi

c - c中的返回函数主菜单

转载 作者:行者123 更新时间:2023-11-30 17:04:08 24 4
gpt4 key购买 nike

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

代码:

#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();
}

请帮我写返回代码行,提前致谢

最佳答案

这里有一些代码可以帮助您了解如何返回值的函数的机制,在您的情况下返回到主函数。

至于一些建议,请阅读有关魔数(Magic Number)的内容,特别是它们不好的原因。

/*
* 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);
}

关于c - c中的返回函数主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35917794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com