使用c创建菜单

时间:2015-11-15 12:18:57

标签: c-preprocessor

我正在尝试创建菜单,我的菜单列出了a-d。我希望if语句读取菜单中的第一个项目,但我的程序一直在崩溃。我该如何解决这个问题?

#include <stdio.h>
#include <string.h>

int main()
{

     char option[4];



     system("cls");

     printf("+++++++++++++++++++++++++++++++++++++>>>ATHLETE DATA MANAGEMENT SYSTEM<<<++++++++++++++++++++++++++++++++++++++\n\n");
     printf("\t\t\t\t\ta\. Enter Athlete Data\n");
     printf("\t\t\t\t\tb\. Determine Distance to Reach World Record\n");
     printf("\t\t\t\t\tc\. Display Athlete Management Report\n");
     printf("\t\t\t\t\td\. Exit\n");


    printf("Type the corresponding letter (a-d) to access the menu. ");
    scanf("%d", &option);

    if(strcmp(option, "a")==0){
        printf("Welcome to Athlete Data Page\n");
    }
    else if(strcmp(option,"b")==0){
        printf("Determine Distance to Reach World Record\n");

    }else if(strcmp(option, "c")==0){
        printf("Athlete Management Report");

    }else if (strcmp(option, "d")==0){
        printf("Exit Menu");
            printf("Press Y for yes and N for no");
        }
        else{
            printf("Incorrect code entered.");
        }
           return 0;
}

1 个答案:

答案 0 :(得分:0)

您使用的是C风格的字符串变量(字符数组)char option[4];,但使用%d中的scanf将其作为十进制整数读取。您需要%s选项才能执行此操作。

http://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm