2015-04-04 38 views
-1
#include <stdio.h> 
#include <conio.h> 
int main(void) 
{ 
    int year; 
    float principal, amount, inrate, period, value; 
    printf ("Please enter principal"); 
    scanf ("%f", principal); 
    amount = principal; 
    printf ("Please enter interest rate"); 
    scanf ("%f", inrate); 
    year = 0; 
    printf ("Please enter period"); 
    scanf ("%f", period); 
     while(year <= period) 
      {printf ("%d %f\n", year, amount); 
      value = amount + amount*inrate; 
      year = year + 1; 
      amount = value; 
      } 
     getch(); 
    return 0; 
} 

我試着運行這段代碼,但是我根本沒有輸出。有0個警告和消息。坦率地說,我不知道代碼是否能夠達到預期的目的而不能運行它!請幫忙。我的簡單代碼沒有給出輸出

+3

記住使用'&'和'scanf'。 – teppic 2015-04-04 19:28:47

+0

感謝它的工作。 – SirVirgin 2015-04-04 19:31:22

+1

用'-Wall'選項編譯。 – BLUEPIXY 2015-04-04 19:32:15

回答

0

您遇到的問題是雙重的。第一個,scanf需要一個指針來存儲值。 (例如scanf ("%f", principal);應該scanf ("%f", &principal);

另一個問題需要注意的是讀值隨scanf每次按[Enter]時會留下一個換行符'\n'輸入緩衝區stdinscanf會讀取您輸入的號碼,但將換行符保留在stdin。下次您撥打scanf時,它會在stdin中看到換行符(值:0xa十六進制,10),並將其作爲下一個值讀取。

注意:在這種情況下,%f將跳過換行,所以這是沒有必要的。但是,請注意,由scanf讀取的小數或字符串將會生效。在使用scanf時請始終記住這一點。

如果遇到scanf似乎會跳過預期的輸入,一個簡單的解決方案是清空(空)輸入緩衝區。 (下面的函數flush_stdin提供瞭如何處理這個問題的一個例子)。在每次致電scanf後,請致電flush_stdin,這是潛在的問題。

#include <stdio.h> 
// #include <conio.h> 

void flush_stdin() 
{ 
    int c = 0; 
    while ((c = getchar()) != '\n' && c != EOF); 
} 

int main(void) 
{ 
    int year = 0; /* Always INITIALIZE your variables */ 
    float principal, amount, inrate, period, value; 
    principal = amount = inrate = period = value = 0; 

    printf ("Please enter principal: "); 
    scanf ("%f", &principal); 

    amount = principal; 

    printf ("Please enter interest rate: "); 
    scanf ("%f", &inrate); 

    year = 0; 

    printf ("Please enter period: "); 
    scanf ("%f", &period); 

    while(year <= period) 
    { 
     printf ("%3d %10.2f\n", year, amount); 
     value = amount + amount*inrate; 
     year = year + 1; 
     amount = value; 
    } 

    // getch(); 

    return 0; 
} 

輸出

$ ./bin/scanf_noop 
Please enter principal: 123.45 
Please enter interest rate: .05 
Please enter period: 24 
    0  123.45 
    1  129.62 
    2  136.10 
    3  142.91 
    4  150.05 
    5  157.56 
    6  165.43 
    7  173.71 
    8  182.39 
    9  191.51 
10  201.09 
11  211.14 
12  221.70 
13  232.78 
14  244.42 
15  256.64 
16  269.48 
17  282.95 
18  297.10 
19  311.95 
20  327.55 
21  343.93 
22  361.12 
23  379.18 
24  398.14 
+0

'%f'格式跳過空白區域,如換行符,空格和製表符,沒有任何問題。 'flush_stdin()'調用在這裏是過度的,特別是因爲你沒有檢查輸入是否工作。這將作爲錯誤處理策略的一部分是有意義的。 – 2015-04-04 22:15:54

+0

授予。這是連續調用中的混音字符串和數字會導致你死亡。但是,當提供幫助新人的例子時,矯枉過正並不全是壞事。我會放下一張紙條。謝謝。 – 2015-04-04 22:18:21

1

scanf從標準輸入讀取數據,並根據參數格式將它們存儲到附加參數指向的位置。所以如果你想用scanf在變量中保存一些東西,你應該把指針作爲參數與&。

1

scanf()調用中的可變參數之前添加&地址之後,它可以工作。但我沒有檢查算術。

#include <stdio.h> 
#include <conio.h> 

int main(void) 
{ 
    int year; 
    float principal, amount, inrate, period, value; 
    printf ("Please enter principal "); 
    scanf ("%f", &principal);     // <-- added & 
    amount = principal; 
    printf ("Please enter interest rate "); 
    scanf ("%f", &inrate);      // <-- added & 
    year = 0; 
    printf ("Please enter period "); 
    scanf ("%f", &period);      // <-- added & 
    while(year <= period) { 
     printf ("%d %f\n", year, amount); 
     value = amount + amount*inrate; 
     year = year + 1; 
     amount = value; 
    } 
    getch(); 
    return 0; 

}

2

我嘗試運行此代碼,但我必須在所有

真的沒有輸出?我有6個警告和段錯誤!你在用什麼編譯器?

 ||=== Build: Debug in test (compiler: GNU GCC Compiler) ===| 
     main.cpp||In function 'int main()':| 
     main.cpp|8|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]| 
     main.cpp|11|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]| 
     main.cpp|14|warning: format '%f' expects argument of type 'float*', but argument 2 has type 'double' [-Wformat]| 
     main.cpp|8|warning: 'principal' is used uninitialized in this function [-Wuninitialized]| 
     main.cpp|11|warning: 'inrate' is used uninitialized in this function [-Wuninitialized]| 
     main.cpp|14|warning: 'period' is used uninitialized in this function [-Wuninitialized]| 
     ||=== Build finished: 0 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===| 

代碼看起來像某種利益計算器(https://en.wikipedia.org/wiki/Interest

嘗試代碼:

#include <stdio.h> 
    #include <conio.h> 
    int main(void) 
    { 
     int year; 
     float principal, amount, inrate, period, value; 
     printf ("Please enter principal "); 
     scanf ("%f", &principal); 
     amount = principal; 
     printf ("Please enter interest rate "); 
     scanf ("%f", &inrate); 
     year = 0; 
     printf ("Please enter period "); 
     scanf ("%f", &period); 
      while(year <= period) 
       { 
       printf ("%d %f\n", year, amount); 
       value = amount + amount*inrate; 
       year = year + 1; 
       amount = value; 
       } 
      getch(); 
     return 0; 
    }