2014-02-28 34 views
0

我無法獲得與用戶輸入一起工作的指數。每次提示用戶添加輸入時,在輸入輸入後,程序立即關閉。處於指數狀態的問題

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

int main() 
{ 
    double input = 0; 
    double exp_val; 
    printf("Plese enter the exponent to check for convergence:"); 
    scanf("%f", input,"\n"); 
    printf("%f", input);/*Checking to verify the input is entered*/ 
    exp_val = exp(input); 
    printf("%f", exp_val); 
    getchar(); 
} 

回答

1

除了@ happydave的答案使用&input,你還需要%lf格式說明讀雙:

scanf("%lf", &input); 

退房Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?


在這之後,你應該得到正確的答案,看到它住:http://ideone.com/246clu

+0

打印語句是否需要保持%f,因爲它將立即關閉後用戶輸入輸入 – user3259144

+0

printf不需要%lf。看到這個答案:http://stackoverflow.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f – happydave

+0

@ user3259144檢查代碼(運行結果)由新的鏈接。 – herohuyongtao

1

scanf的期望的指針雙,而不是雙本身:

scanf("%lf", &input); 

這是因爲通過直接輸入由值傳遞。但是scanf不想知道輸入的當前值,它想知道它應該在哪裏寫入它讀取的數據的內存位置。

+0

我添加了指針,它仍然崩潰。初始化時,我需要對雙精度進行操作嗎? – user3259144

+1

你有沒有嘗試%lf,正如@ herohuyongtao正確指出的那樣? – happydave

+0

是的,我試過了,它不會保持打開狀態 – user3259144