2017-07-27 38 views
-1

在我輸入值pq,scanf然後輸入printf之後,獲得了可笑的價值。爲什麼?獲取更多我的雙重價值

void gyllenSnitten(void); 
void Andragradare(void); 

double p,q; 
double root1, root2; 

void main() 
{ 
    gyllenSnitten(); 
    Andragradare(); 
} 

void gyllenSnitten(void){ 
    printf("Gyllende snittet är %d..\n",(1+sqrt(5))/2) 
} 

void Andragradare(void){ 

    system("cls"); 

    printf("P-q formeln,\n"); 
    printf("Choice youre P \n"); 

    scanf_s("%1f",&p); 
    printf("Choice youre Q\n"); 
    printf("%1f", &q); 

    root1 = (-p/2) + sqrt(((p/2)*(p/2)) + q); 
    root2 = (-p/2) - sqrt(((p/2)*(p/2)) + q); 

    printf("Here is the first X = %d",root1); 
    printf("and the other onehere X = %d",root2); 

    _getch(); 
} 
+4

'%1f'應該是'%lf','%d'應該是'%f'。 –

+0

關於'printf'和'scanf'的文檔有什麼不清楚? – Olaf

+0

改變這一點,但仍然沒有workning,我看着debugg一步,並獲得價值一樣 &P \t 0x00c6a140 {labb3e.exe!雙P} {5.356796015279e-315#DEN} \t雙對我的 「P」 –

回答

2

%d格式說明用於printf()需要int型參數,傳遞的任何其它類型是未定義的行爲。

嘗試%f打印double s。

+0

'%F'爲打印'雙'?它不應該是'%lf'嗎? –

+1

@J ... S文檔很明確:兩者都是有效的(兼容性原因,您不能將'float'作爲可變參數傳遞。 – Olaf