薄苏 2022-12-24 23:05 采纳率: 100%
浏览 20
已结题

关于#c语言#的问题:我不理解为啥输出完100个字母之后会输出汉字

我不理解为啥输出完100个字母之后,会继续输出几个汉字哇?有没有解决方法各位亲,谢谢各位了orz

void practice() {
    char putout[100]{};
    srand(time(NULL));
    int i, j;
    for (i = 0; i < 100; i++) {
        putout[i] = rand() % 25+1 + 'a';
    }
    printf("%s", putout);
}

int main() {
    while(1) {
        printf("是否开始打字训练?1(继续)/2(取消)\n");
        int n;
        char input[100];
        scanf_s("%d", &n);
        if (n == 1) {
            practice();
            scanf("%s", &input);
        }
    }
}

img

  • 写回答

2条回答 默认 最新

  • 会修bug的猫 2022-12-24 23:31
    关注

    字符串末尾没 '\0'
    还有一些其他的小毛病
    稍微帮你修改了一下

    void practice() {
        char putout[101];
        int i, j;
        for (i = 0; i < 100; i++) {
            putout[i] = rand() % 25 + 1 + 'a';
        }
        putout[100] = '\0';
        printf("%s\n", putout);
    }
    int main() {
        srand(time(NULL));
        while (1) {
            printf("是否开始打字训练?1(继续)/2(取消)\n");
            int n;
            char input[101];
            scanf_s("%d", &n);
            getchar();
            if (n == 1) {
                practice();
                scanf("%s", &input);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月2日
  • 已采纳回答 12月25日
  • 创建了问题 12月24日