gpt4 book ai didi

c - 为什么以下代码在使用 system ("cls"后会改变其行为?

转载 作者:行者123 更新时间:2023-11-30 15:07:29 24 4
gpt4 key购买 nike

#include<stdio.h>

int main() {
//system("cls");
char randomStr[] = "0123456789";
printf("\n\n\n\n\t\t\t\t\t");
while(!kbhit()) {
int i;
for(i = 0;i < 10;i++) {
printf("%c", randomStr[i]++);
while(randomStr[i] == '\n' || randomStr[i] == '\r' || randomStr[i] == '\t' ||
randomStr[i] == '\b' || randomStr[i] == '\0' || randomStr[i] == '\a' ||
randomStr[i] == '\v' || randomStr[i] == '\f') {

randomStr[i]++;
}
}
printf("\b\b\b\b\b\b\b\b\b\b");
}
return 0;
}

我希望光标向右移动一点并开始打印字符,然后覆盖它们自身,从而产生一种滚动效果(就像在彩票机中一样)。这在不使用 system("cls") 的情况下工作正常,但如果我使用 system("cls") 光标将被拉回到行的开头。图片如下:

没有 system("cls") 它工作正常(图像中的字符是随机的,在程序运行时的实例中)

without system("cls")

添加system("cls")后会发生这种情况(突出显示的区域是我想要的效果发生的地方)

with system("cls")

最佳答案

system("cls") 是一个系统调用,用于清除相关控制台上的屏幕。它的行为方式与在 Windows 命令行上用作 cls 的方式相同(尝试一下)。因此,这将从屏幕上清除所有当前字符,使您没有您正在寻找的“滚动效果”。

关于c - 为什么以下代码在使用 system ("cls"后会改变其行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com