You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言图形绘制程序执行scanf_s读取字符时无报错崩溃如何解决

问题根因

你遇到的运行终止问题由两个scanf_s的使用错误共同导致:

  • 第一个scanf_s("%d", &value)执行后,用户输入数字按下的换行符会残留在输入缓冲区中。第二个scanf_s("%c", &type)会直接读取这个换行符,不会等待用户输入新字符,程序会直接向下执行完所有逻辑退出,给人「提前终止」的错觉。
  • 微软扩展的scanf_s安全函数读取%c格式字符时,必须额外传入缓冲区长度作为第三个参数,你未传入该参数会触发运行时安全检查,直接终止程序,不会返回错误码。
修复方案
  1. 在第二个scanf_s的格式符前加空格,跳过输入缓冲区里的所有空白字符(包括残留换行)
  2. 给读取char的scanf_s补充长度参数1
  3. 可选优化:倒三角的循环条件多执行了一次,将while (firstcounter <= value2)改为while (firstcounter < value2)避免多输出一行空行;main函数末尾增加return 0符合C语言标准。
修正后完整代码
#include <stdio.h>

int main() {
    char type;
    int value=0;

    printf("Enter Shape Choice: 1 Inverted triangle, 2 Square, 3 Slope, 4 Diamond with Marked Edges\n");
    scanf_s("%d", &value);

    printf("Enter Charachter to draw with:\n");
    // 加空格跳过空白符,补充长度参数1
    scanf_s(" %c", &type, 1);

    int count = 0;
    int count2 = 0;
    int firstcounter = 0;
    int value2 = 0;
    if (value == 1) {
        printf("please enter how many lines you would like:");
        scanf_s("%d", &value2);
    }

    switch (value) {
        case(1):
            // 修正循环条件避免多输出一行
            while (firstcounter < value2) {
                for (int i = value2; i > firstcounter; i--) {
                    printf("*");
                }
                printf("\n");
                firstcounter++;
            }
            break;

        case(2):
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    printf("%c", type);
                }
                printf("\n");
            }
            break;

        case(3):
            for (int i = 0; i < 5; i++) {
                count2 = 5;
                while (count2 > i) {
                    printf(" ");
                    count2--;
                }
                count = 0;
                printf("%c", type);
                while (count <= i - 1) {
                    printf("*");
                    count++;
                }
                printf("\n");
            }
            break;

        case(4):
            printf("    %c", type);
            int seccount = 1;
            for (int i = 0; i <= 4; i++) {
                int firstcount = 1;
                int thirdcount = 10;
                printf("\n");
                if (i < 3) {
                    for (int k = 3; k > i; k--) {
                        printf(" ");
                    }
                    printf("%c", type);
                    while (firstcount <= seccount) {
                        printf("*");
                        firstcount++;
                    }
                    printf("%c", type);
                    seccount = seccount + 2;
                }
                if (i >= 3) {
                    for (int k = 1; k < i; k++) {
                        printf(" ");
                    }
                    printf("%c", type);
                    while (thirdcount > seccount) {
                        printf("*");
                        thirdcount--;
                    }
                    seccount = seccount + 2;
                    printf("%c", type);
                }
            }
            printf("\n    %c", type);
            break;
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Banks-e

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 05:09:01