【问题标题】:C: Insert symbols to be displayed after the input cursorC:在输入光标后插入要显示的符号
【发布时间】:2013-02-09 09:10:33
【问题描述】:

我从用户那里得到年利率。我希望在用户输入利率时显示“%”。它应该是这样的:

Enter the annual interest rate: %

注意悬挂的“%”。光标应该在“%”之前(或在哪个位置)闪烁,这样当用户键入 2.9 时,它看起来像这样:

Enter the annual interest rate: 2.9%

然后,用户按 ENTER 键,代码在新行上照常继续。

这在 C 语言中是否可行?如果是这样,我该怎么做?

【问题讨论】:

  • 我认为这不可能作为控制台程序。您可以使用一些 GUI 来完成。您可以随时提示“输入年利率(%):”来提醒用户。
  • 在控制台上可以使用_clrscr()和_getch(),但是不太实用。
  • @Armin 我认为这不适用于 Windows 以外的平台。
  • 这在标准 C I/O 库中是不可能的。它旨在与电动打字机和穿孔胶带配合使用,而不是它们新奇的 CRT 东西。你需要一个终端 I/O 库,例如 GNU Readline(原生用于 Unix,但我认为也有 Windows 版本)。

标签: c printf scanf


【解决方案1】:

如果您只想在命令行终端中运行程序,可以使用gets() 调用。 注意 printf 中需要双 % 才能输出一个 % 符号。

 double percentRate;
 printf("Enter the annual interest rate: %%");
 char input[256];
 gets(input);
 // then parse the input buffer
 percentRate = atof(input);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2019-03-29
    • 2015-10-18
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多