【问题标题】:Receiving Run-Time Check Failure #2接收运行时检查失败 #2
【发布时间】:2025-12-22 10:40:12
【问题描述】:

我想问一下为什么我在执行程序时会遇到运行时检查失败 #2? 我对 C 编程很陌生。

我正在尝试制作一个控制台应用程序,在输入 Y/N 后有一些选项, 但是每当我到达所有选项的末尾时,我都会收到该错误。

谁能告诉我如何解决它以及进行这种编程的正确方法是什么?

#define _CRT_SECURE_NO_WARNINGS // To allow Visual studio to use "scanf" function
#include <stdio.h> // Standard Input output . header
#include <Windows.h>

void codername() {
    printf("Coder: Rong Yuan\n");
}

void projectname() {
    printf("Project name: NPoly Learning\n");
}

void loadcurrentdate() {
    SYSTEMTIME str_t;
    GetSystemTime(&str_t);

    printf("Date: %d . %d . %d \n"
        , str_t.wDay, str_t.wMonth, str_t.wYear);
}

int main() {
    char option;
    int input;
    int mincome, fmember, total;

    printf("Do you like to see our option? Y/N \n");
    scanf("%s", &option);
    if (option == 'y' || option == 'Y') {
        printf("1. Display Coder Detail\n");
        printf("2. Display Project Name\n");
        printf("3. Load Current Date\n");
        printf("4. Calculator PCI\n");
        printf("5. Exit\n");
        scanf("%d", &input);
    }
    else
        exit(1);

    switch (input) {
        case 1:
            codername();
            printf("Do you like to return to main?");
            break;

        case 2:
            projectname();
            break;

        case 3:
            loadcurrentdate();
            break;

        case 4:
            printf("Enter your house monthly income: ");
            scanf("%d", &mincome);
            printf("Enter total family member: (INCLUDING YOURSELF) ");
            scanf("%d", &fmember);
            total = mincome / fmember;
            printf("Total PCI: %d / %d = %d \n", mincome, fmember, total);
            system("pause");
            break;

        case 5:
            exit(0);
    }
}

【问题讨论】:

  • 请将错误信息复制到您的帖子中^^

标签: c compiler-errors


【解决方案1】:
scanf("%s", &option);

错误,因为 optionchar 。因此,将%s 替换为%c%s 应该用于字符串(字符数组),%c 是用于字符的格式说明符。

【讨论】: