【问题标题】:Menu system in cc中的菜单系统
【发布时间】:2016-07-16 02:54:46
【问题描述】:

我的程序遇到了严重问题,它应该提供一个菜单并执行所有功能,代码很容易解释我的问题是我只有视觉工作室,它不允许 scanf 和 scanf_s 并且会弄乱东西,所以我使用在线编译器,但那些仍然不确定。任何人都可以帮助并给我一些提示。我在列出帐户的数学和函数方面遇到了麻烦,有些时候也是空的,我不确定这是否最好用。我对这个论坛比较陌生。它也不能有结构,只能在 C 中:-(

#include <stdio.h>

char name[20];
float avail_bal;

void options();
void open();
void list();
void deposit();
void withdraw();
void exit();

int main(void)
{
    char option;
    while(1){
        printf("****Banking System WELCOME****\n");
        printf("Enter 1-5 of the following options: \n");

        option = getchar();
        scanf("%c\n", &option);
        switch(option)
        {
            case '1': open();
            break;
            case '2': list();
            break;
            case '3': deposit();
            break;
            case '4': withdraw();
            break;
            case '5': return 0;
            default: exit();
            break;
        }
    }
    return 0;
}

void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}

void open()
{
    float avail_bal = 0;
    char name[20];
    int acc_num;

    printf("Open new account(enter number 1-5)\n\n");
    scanf("%d", &acc_num);
    printf("Account number:     %d\n");
    printf("Available balance:      %f\n");
}

void list()
{
}

void deposit()
{
float add;
int acc_num;

printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;

printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
    printf("Current balance for account %d: %f ");
    break;
} acc_num++
}

【问题讨论】:

  • 您应该为函数和变量使用不同的名称。
  • 就此而言,请使用与标准库已经保留的名称不同的名称,例如 exit
  • 这个问题有很多问题,如果你把它缩小到一个问题会更好

标签: c menu switch-statement


【解决方案1】:

scanf 的问题很有趣。下面是一个示例,说明如何不使用(尽管您不应该这样做),这样您就可以更轻松地使用您的代码。

#include <stdio.h>
#include <stdlib.h>
// only 5 accounts possible
int accounts[5];
// each its balance
float avail_bal[5];

void options();
// open(P) is a standard Posix function
void bopen();
void list();
void deposit();
void withdraw();
// exit(3) is a standard C function
void pexit();

int main(void)
{
  char option;
  while (1) {
    printf("****Banking System WELCOME****\n");
    printf("Enter 1-5 of the following options: \n");
    options();
    option = getc(stdin);
    // swallow the '\n'
    getc(stdin);
    switch (option) {
    case '1':
      bopen();
      break;
    case '2':
      list();
      break;
    case '3':
      deposit();
      break;
    case '4':
      withdraw();
      break;
    case '5':
      pexit();
    default:
      pexit();
    }
  }
  return 0;
}

void options()
{
  puts("1. Open Account");
  puts("2. List Accounts");
  puts("3. Deposit");
  puts("4. Withdraw");
  puts("5. Exit");
}

void bopen()
{
  int acc_num;
  char c;

  puts("Open new account(enter number 1-5)");
  c = getc(stdin);
  getc(stdin);
  // assuming ASCII here where the digits 0-9 start at place 48 in the table
  acc_num = (int) c - 48;
  if (acc_num < 1 || acc_num > 5) {
    puts("Account number must be between one and five inclusive");
    return;
  }
  if (accounts[acc_num] != 0) {
    printf("Account number %d is already taken\n", acc_num);
    return;
  }
  // mark account as taken
  accounts[acc_num] = 1;
  // spend a fiver for the new client for being a new client
  avail_bal[acc_num] = 5.0;
  printf("Account number:     %d\n", acc_num);
  printf("Available balance:      %f\n", avail_bal[acc_num]);
}

void list()
{
  int i;
  for (i = 0; i < 5; i++) {
    if (accounts[i] != 0) {
      printf("Account 000%d:  %f\n", i, avail_bal[i]);
    }
  }
}

void deposit()
{
  float add;
  int acc_num;
  char c;
  char s[100];

  puts("Which account do you want to deposit money in?");
  c = getc(stdin);
  getc(stdin);
  acc_num = (int) c - 48;
  printf("Amount to deposit: ");
  // to get a number without scanf() we have to read the input as a string
  // (fgets() adds a '\0' at the end, so keep a seat free for it)
  fgets(s, 99, stdin);
  // and convert it to a double (atof() only for brevity, use strtod() instead)
  add = atof(s);
  avail_bal[acc_num] += add;
  printf("Amount deposited %f\n", add);

}

void withdraw()
{
  int acc_num;
  float withdraw;
  char c;
  char s[100];

  // all checks ommitted!
  puts("Account to withdraw from: ");
  c = getc(stdin);
  getc(stdin);
  acc_num = (int) c - 48;
  puts("Amount to withdraw from account: ");
  fgets(s, 99, stdin);
  withdraw = atof(s);
  avail_bal[acc_num] -= withdraw;
  printf("Current balance for account %d: %f\n", acc_num, avail_bal[acc_num]);

}

void pexit()
{
  // place logic to save data here or use a function triggered by atexit() for that task
  puts("Imagine all of your data would have been put in a safe place!");
  exit(EXIT_SUCCESS);
}

在您通过作业进行评分之前,只需将结构替换为 scanf

【讨论】:

  • 只是提一下,使用struct会更容易理解:)
  • 所以要用 fgets 替换 scanf 我只需做 scanf("%f", add) 对吗?我也可以使用 getchar() 而不是 getc(stdin) ,不是吗?另一件事是什么?谢谢
  • fgets()scanf() 不同;您可以将getc() 替换为getchar(stdin),两者都可以实现为宏,函数fgetc() 不是; atof()strof() 的简化版,主要区别在于缺少错误检查。您可以在手册页中获取所有这些信息。如果您没有安装(例如:在 Windows 上),您可以在网上找到它们,例如:linux.die.net/man 向 Google 提供示例也是一个好主意,例如:C atof example 并解析结果。
  • 我可以去掉 atof 并替换简单地在 add=0 处初始化并退出 =0 吗?我的编译器说可能丢失数据
  • fgets() 为您提供了一个字符串,如果您想将其用作数字,则必须将其转换为数字。这就是我提到用scanf() 替换所有这些结构的原因,它会为您完成。或者您自己编写一个函数,将字符串转换为数字。为整数做“简单”,为浮点做 非常 困难。但是,如果我误解了你并且你只想要一个占位符,那就去吧(但使用 0.0 代替,应该摆脱警告,虽然我不太了解你的编译器)。
【解决方案2】:

如果你使用整数值作为选项为什么你使用字符的

#include <stdio.h>

char name[20];
float avail_bal;

void options();
void open();
void list();
void deposit();
void withdraw();
void exit();

int main(void)
{
        int option;
        printf("****Banking System WELCOME****\n");
        void options();
        printf("Enter 1-5 of the following options: \n");

        scanf("%d",&option);

        switch(option)
        {
            case 1: open();
            break;
            case 2: list();
            break;
            case 3: deposit();
            break;
            case 4: withdraw();
            break;
            case 5: return 0;
            default: exit();
            break;
        }

    return 0;
}

void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}

void open()
{
    float avail_bal = 0;
    char name[20];
    int acc_num;

    printf("Open new account(enter number 1-5)\n\n");
    scanf("%d", &acc_num);
    printf("Account number:     %d\n");
    printf("Available balance:      %f\n");
}

void list()
{
}

void deposit()
{
float add;
int acc_num;

printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;

printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
    printf("Current balance for account %d: %f ");
    break;
} acc_num++
}

【讨论】:

  • 什么意思?对不起,我是菜鸟,你的意思是 int main 吗?无效选项仅用于显示菜单
  • 我想说的是,如果你想从用户那里获取 1 到 5 之间的值,你可以简单地使用一个整数来比较常量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2011-11-13
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
相关资源
最近更新 更多