【问题标题】:Update previous line instead print new line更新上一行而不是打印新行
【发布时间】:2016-09-11 09:01:35
【问题描述】:

这是一个简单的c程序。它需要两个整数并将它们相加。在这段代码中,我想逐行更新前一行,而不是制作一个新的,任何人都可以帮助我解决这个问题。

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b;
printf("Enter two integer to add\r");
scanf("%d%d",&a,&b);
a=a+b;
printf("Your value is -- %d",a);

return 0;
}

我使用 \r 而不是 \n,将光标带回起点。我在程序控制台中有输入数据,从一开始就写入。但是下一行“您的值是..”打印为新行,我想删除第一行,然后输入值并打印下一行“您的值..”。我怎么做?请帮帮我

【问题讨论】:

  • 简答:你不能(很容易)。长答案:您想要的不是标准 C 语言或运行时的一部分。像ncurses 这样的东西是一种选择,但坦率地说,就像用大锤敲击图钉一样。

标签: c printf


【解决方案1】:

你不能像那样在终端中向上移动,除非使用一些复杂的库作为诅咒。 您可以使用“清屏”技巧,也许可以达到您想要的效果。

#include <stdio.h>
#include <stdlib.h>

    int main()
    {
    int a,b;
    system("clear"); // or "cls" on windows
    printf("Enter two integer to add\r");
    scanf("%d%d",&a,&b);
    system("clear"); // or "cls" on windows
    a=a+b;
    printf("Your value is -- %d",a);

    return 0;
    }

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多