c - 使用 do 循环和函数来制作菜单。 C

标签 c

我的代码中似乎有什么问题?我希望制作一个程序,允许用户从选项/菜单列表中进行选择。用户选择一个并运行一个函数。函数执行后,菜单会再次出现,直到用户决定退出。

我的代码如下:

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

 int evaluate(int num);
 int binaryPrinter(int dec);
 int hexaPrinter(int dec);
 int militaryTime(int hh, int mm);

 int main(void)
 {
         int choice, x, y;

    do {
        printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
        scanf("%d, &choice");
        (void) getchar();

        switch (choice){
        case 1:
            printf("Enter number to be evaluated:");
            scanf("%d, &x");
            int evaluate(int x);
            break;
        case 2:
            printf("Enter a decimal number: ");
            scanf("%d, &x");
            int binaryPrinter(int x);
            break;
        case 3:
            printf("Enter a decimal number: ");
            scanf("&d, &x");
            int hexaPrinter(int x);
            break;
        case 4:
            printf("Enter time in standard format: ");
            scanf("%d:%d, &x, &y");
            int militaryTime(int x, int y);
            break;
        default:
            printf("Invalid choice. Please choose only among the choices.");

        }
    }while(choice != 5);
return 0;
}

这是输入选择编号后显示的内容:Process returned -1073741819 <0xC0000005>

-编辑-

我已经认识到自己所犯的错误。但结果还是一样。

这是我的新代码,但同样的错误:Process returned -1073741819 <0xC0000005>

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

int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);

int main(void)
{
    int choice, x, y;

    do {
        printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
        scanf("%d", &choice);
        (void) getchar();

        switch (choice){
        case 1:
            printf("Enter number to be evaluated:");
            scanf("%d", &x);
            evaluate(x);
            break;
        case 2:
            printf("Enter a decimal number: ");
            scanf("%d", &x);
            binaryPrinter(x);
            break;
        case 3:
            printf("Enter a decimal number: ");
            scanf("%d", &x);
            hexaPrinter(x);
            break;
        case 4:
            printf("Enter time in standard format: ");
            scanf("%d:%d", &x, &y);
            militaryTime(x, y);
        default:
            printf("Invalid choice. Please choose only among the choices.");

        }
    }while(choice != 5);
return 0;
}

最佳答案

试试这个代码

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

int evaluate(int num);
int binaryPrinter(int dec);
int hexaPrinter(int dec);
int militaryTime(int hh, int mm);

int main(void)
 {
     int choice, x, y;

  do
   {
    printf("Type the number of your choice:\n1: Place value evaluation\n2: Convert a Decimal to Binary\n3: Convert a Decimal to Hexadecimal\n4: Convert Standard time to Military time\nEnter Number of Choice: ");
    scanf("%d", &choice);
    (void) getchar();

    switch (choice){
    case 1:
        printf("Enter number to be evaluated:");
        scanf("%d", &x);
        evaluate(x);
        break;
    case 2:
        printf("Enter a decimal number: ");
        scanf("%d", &x);
        binaryPrinter(x);
        break;
    case 3:
        printf("Enter a decimal number: ");
        scanf("&d", &x);
        hexaPrinter( x);
        break;
    case 4:
        printf("Enter time in standard format: ");
        scanf("%d:%d", &x, &y);
        militaryTime( x, y);
        break;
    default:
        printf("Invalid choice. Please choose only among the choices.");

    }
   }while(choice != 5);
  return 0; 
}

您使用了错误的 scanaf() 语法

它不像scanf("%d,&choice");

就像scanf("%d",&choice);

并且还错过了函数调用的语法

它不像intvaluate(int x);,它就像evaluate(x)

关于c - 使用 do 循环和函数来制作菜单。 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36076879/

相关文章:

c - 字符串和无限循环中的堆栈粉碎

自定义区域语言

c - 并行区域内的这个指针变量是共享的还是私有(private)的?

c - 在 C 中的其他输出之间打印数组元素

c++ - 显式分配或访问内存中特定地址/位置的值或从中访问值?

C语言-scanf更多输入但不知道有多少输入

c - 我们需要在 wordexp 失败时调用 wordfree 吗?

C编程。如何制作打印字符串的方法

c++ - 在 Ubuntu Linux 中使用 Nvidia 卡的 OpenCL 出现 -1001 错误

我可以为自定义日志记录功能启用格式警告吗?