【问题标题】:Simple menu in cc中的简单菜单
【发布时间】:2017-07-14 19:06:38
【问题描述】:

我是一名新计算机科学专业的学生,​​目前正在研究一个关于超级商店管理系统的大学项目。

为此,我想要一个菜单​​系统。我尝试使用开关盒。
它可以工作,但是,例如,当我按下1 时,程序会转到我定义的另一个函数。

但主菜单功能仍然出现在顶部。
我想要一个菜单​​,我将在其中按一个数字,程序将继续执行某个功能,清除我以前使用的命令。

任何帮助将不胜感激。谢谢。

(我尝试使用 switch case。而且我是新手,对文件处理之类的东西一无所知)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>


//User defined function declaration

int main_menu();
int products();
double payment();
int customer();

//User defined sub menus



//under products

int home_products();
int electronics();

int add_products();
int remove_products();
int products_quantity();

//Under payment

double billing();

//under customer menu()

int customer_inf();
int edit_customer();




int main ()
{
int input;

printf("                              Hello ADMIN\n                          SHIRONAMHIN Super Shop\n================================================================================");
printf("                              -Products\n                              -Payment/Billing\n                              -Customer information\n");
printf("\n");
printf(" Press One for Products \n Press two for Billing \n Press Three for Customer information \n Press 0 to terminate program");
scanf("\n%d", &input);
if(input==0)
{
    return 0;  //Program will be terminated if 0 is pressed
}
switch(input)
{
case 1:
    if(input==1)
    {
        products();
    }
    break;
case 2:
    if(input==2)
    {
        payment();
    }
    break;
case 3:
    if(input==3)
    {
        customer();
    }
    break;

default:
    printf("Enter a valid number");
    break;
}
 }


  int products()

  {
    int input;
printf("\n                              -Home products\n                              -Electronics\n\n Press any key rather than 1 and 2 two return to main menu");

scanf("%d", &input);

switch (input)
{
case 1:
    if(input == 1)
        home_products();
    break;

case 2:
    if(input == 2)
        electronics();
    break;

default:
    main();
    break;
}

   }

   int home_products()
     {
      int input;
         printf("Code yet to be written\n\n return to main menu by pressing 1 on the keyboard");
    scanf("%d", &input);
      if(input == 1)
       {
         return main();
     }


      }

   int electronics()
       {
      printf("Code yet to be written");
        }

      double payment()
      {

        return main();

      }

      int customer()
          {
     printf("Code yet to be written");

      return main();
          }

【问题讨论】:

  • 如果您不在此处发布代码,我们无法帮助您了解为什么您的代码无法正常工作。如果您无法开始作业,请向您的教师寻求帮助。
  • 你能贴出你目前尝试过的代码吗?
  • 已添加@JosephSible 代码
  • 你想清除上一屏,要么加很多行回车(简单),要么使用终端库清除窗口

标签: c console console-application


【解决方案1】:

你可以看看这个Answer。我已经在 Windows 上测试过了。

基本上是这样的

在 Windows(cmd.exe)、Linux(Bash 和 zsh) 和 OS X(zsh) 上测试的解决方法:

你可以有这样的功能

void clrscr()
{
    system("cls||clear");
}

system("cls")(在 Windows 上工作)的基本问题是它不可移植,system("clear")(在 Linux 上工作)也是如此。

以上功能适用于两者。

你可以这样拥有它

case 1:
    if(input==1)
    {
        clrscr();
        products();
    }

所以如果input == 1,首先会调用clrscr()并清除控制台,然后再调用products()。你可以对所有其他函数做同样的事情,不要忘记在上面包含函数clrscr()的定义。

此外,我强烈建议您查看此问题How do you clear the console screen in C? 了解有关此主题的更多信息。

【讨论】:

猜你喜欢
  • 2010-09-22
  • 2021-11-07
  • 2012-08-29
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2015-02-20
  • 2010-12-03
相关资源
最近更新 更多