gpt4 book ai didi

c - 扫描文本迫使我进入下一行,以确认输入。对于特殊情况我可以阻止这种情况吗?

转载 作者:行者123 更新时间:2023-11-30 17:40:18 25 4
gpt4 key购买 nike

所以基本上我有一个 C 程序,其要点是看起来有条理。我希望程序的光标在确认 Scanf 语句后不要移动。这是我的程序的示例部分及其输出:

#include <stdio.h>
int main()
{
char input[10];
int voltage=220;
printf("What do you want to know\n"); //no need to correct me on this part please
scanf("%s", input);
if (strcmp("voltage",input)==0)
printf(" = %d\n", voltage);
/* Imagine other string comparisons (resistance, current) here*/
return 0;
}

因此,这个输入“电压”的程序将输出如下内容:电压//我刚刚通过scanf输入的值 = 220//\n 因为我输入后按了回车键。

我想要的是:

电压 = 220

我不知道是否有我不知道的反向\n 函数。

最佳答案

C 中没有标准方法可以在不按 Enter 的情况下读取输入。您必须选择特定于平台的功能。

StackOverflow 中有很多涉及此问题的问题,例如 Capture characters from standard input without waiting for enter to be pressed

关于c - 扫描文本迫使我进入下一行,以确认输入。对于特殊情况我可以阻止这种情况吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564238/

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