2013-03-28 36 views
2
#include <stdio.h> 

int main() 
{ 
    int answer; 
    printf("Please insert your desired budget :"); //normal printf functions. 
    printf(" $_____\b\b\b\b"); //This should move the curser back 4 spaces. 
    //The program outputs the line followed with 4 inverted question marks. 
    scanf("%d", &answer); 
    printf("So your budget is %d", answer); 
    return 0; 
} 

輸出是4個反轉問號?我在Mac上使用xcode,可以解決這個問題嗎?我的代碼中的 b不會將光標移回

+0

有幾件事:1)我會想象使用浮動類型的錢。 2)第二個'printf'中有5個下劃線。 3)你應該對輸入做一些錯誤檢查。 3)你可能需要在最後一個打印中使用'\ n'。否則,這看起來應該起作用。 – chrsblck

+1

你在Mac上使用哪個版本的gcc?它在Ubuntu上運行良好。 (錯誤地,你插入了5個下劃線而不是4個),無論如何,它對我的​​工作很好。 –

回答

3

您需要在支持\b轉義序列的終端環境中運行它。 Xcode中的控制檯一定不能理解它們。

如果您在終端應用程序中運行它,它應該沒問題。

+0

+1。對於終端上的「血淋淋的細節」:http://linusakesson.net/programming/tty/index.php –

1

有書中[C的Primer Plus] 6 Page.91

list3.10 escape.c爲例

/* escape.c -- uses escape characters */ 
#include <stdio.h> 


int main(void) 
{ 
    float salary; 
    printf("\aEnter your desired monthly salary:");/* 1 */ 
    printf(" $_______\b\b\b\b\b\b\b"); /* 2 */ 
    scanf("%f", &salary); 
    printf("\n\t$%.2f a month is $%.2f a year.", salary,salary * 12.0); /* 3 */   
    return 0; 
} 

作者說:

的實際行爲可能會有所不同。例如,XCode 4.6將\ a,\ b和\ r字符顯示爲顛倒的問號!

他們顯示的Xcode不支持\b 和蘋果Apple Support Communities也顯示了這種情況。

在我的Xcode 5.0中,它有相同的情況。 enter image description here

相關問題