2016-09-11 25 views
2

這是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,將光標移回到開始位置。我從程序控制臺輸入數據,從頭開始寫。但是下一行「Your value is ..」打印爲新行,我想刪除第一行然後輸入值並打印下一行「Your value ..」。我怎麼做?請幫我

+2

簡答:你不能(很容易)。長的答案:你想要的不是標準C語言或運行時的一部分。像[ncurses](https://en.wikipedia.org/wiki/Ncurses)是另一種選擇,但坦率地說,就像用大錘開着圖釘。 – WhozCraig

回答

1

你不能在終端上移動,除非使用一些複雜的lib作爲詛咒。 你可以使用「清屏」技巧,也許這將實現你想要的。

#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; 
    } 
+0

我可以從慾望點清除屏幕 – Kumar

+1

不,因爲你需要詛咒但不去那裏:) –