2013-02-08 33 views
-2

編程新手,但在編程課程即時開始介紹它。問題是他們沒有教它,他們只是給我們做的事情。我們需要創建函數來計算某人基於一個人擁有的硬幣和美元數量(通過用戶輸入)的美分數量。我不知道我在做什麼,或者即使使用函數原型的權利,但是當我運行我的程序我沒有得到任何錯誤,但它只是打印一個大的負數。繼承人我有什麼,我知道它很抱歉,但任何幫助,非常感謝。C編程打印大負數

#include <stdio.h> 
#include <stdlib.h> 
#include </home/TU/tue64800/include/myheader.h> 

#define PENNYCOIN  1 
#define NICKELCOIN  5 
#define DIMECOIN  10 
#define QUARTERCOIN 25 
#define HALFCOIN  50 
#define DOLLAR  100 

int penny_coin_worth(int number_of_pennies); 
int nickel_coin_worth(int number_of_nickels); 
int dime_coin_worth(int number_of_dimes); 
int quarter_coin_worth(int number_of_quarters);     
int half_coin_worth(int number_of_half);    
int dollar_coin_worth(int number_of_dollars);    
int total_worth (int number_of_pennies, int number_of_nickels, int number_of_dimes, 
int number_of_quarters, int number_of_half, int number_of_dollars); 

int number_of_pennies, number_of_nickels, number_of_dimes, number_of_quarters, 
number_of_half, number_of_dollars; 
int total; 

int main (void) 
{ 
printf("This program was written by %s.\n", PROGRAMMER_NAME); 

printf("Enter number of pennies:\n"); 
scanf("%lf", &number_of_pennies); 

printf("Enter number of nickels:\n"); 
scanf("%lf", &number_of_nickels); 

printf("Enter number of dimes:\n"); 
scanf("%lf", &number_of_dimes); 

printf("Enter number of quarters:\n"); 
scanf("%lf", &number_of_quarters); 

printf("Enter number of half dollars:\n"); 
scanf("%lf", &number_of_half); 

printf("Enter number of dollars:\n"); 
scanf("%lf", &number_of_dollars); 

penny_coin_worth (number_of_pennies); 
nickel_coin_worth (number_of_nickels); 
dime_coin_worth (number_of_dimes); 
quarter_coin_worth (number_of_quarters); 
half_coin_worth (number_of_half); 
dollar_coin_worth (number_of_dollars); 

total_worth (number_of_pennies, number_of_nickels, number_of_dimes, 
number_of_quarters, number_of_half, number_of_dollars); 

return EXIT_SUCCESS; 
} 

int penny_coin_worth (int number_of_pennies) 
{ 
    return (number_of_pennies * PENNYCOIN); 
} 

int nickel_coin_worth (int number_of_nickels) 
{ 
    return (number_of_nickels * NICKELCOIN); 
} 

int dime_coin_worth (int number_of_dimes) 
{ 
    return (number_of_dimes * DIMECOIN); 
} 

int quarter_coin_worth (int number_of_quarters) 
{ 
    return (number_of_quarters * QUARTERCOIN); 
} 

int half_coin_worth (int number_of_half) 
{ 
    return (number_of_half * HALFCOIN); 
} 

int dollar_coin_worth (int number_of_dollars) 
{ 
    return (number_of_dollars * DOLLAR); 
} 

int total_worth (int number_of_pennies, int number_of_nickels, int number_of_dimes, 
int number_of_quarters, int number_of_half, int number_of_dollars) 
{ 
printf("The number of cents is%lf\n", (number_of_pennies * PENNYCOIN +  
number_of_nickels * NICKELCOIN + number_of_dimes * DIMECOIN + number_of_quarters * 
QUARTERCOIN + number_of_half * HALFCOIN + number_of_dollars * DOLLAR)*.01); 
    return (0); 
} 
+1

請寫一個[SSCCE](http://sscce.org)。 –

+3

%lf是雙倍。使用%d作爲int。 –

回答

3

請閱讀printf Format Specifiers。正如Guy Sirton所說,你的錯誤是使用%lf來讀取int(在scanf),當它們是不同的類型時。您應該使用%d作爲int s。

將來,您應該學習如何使用GDB等調試器,並使用它來逐步執行程序,並檢查變量的值。

+0

我通常只是嘗試運行該程序後,我添加了一些新的東西,看看我是否得到錯誤,並感謝生病檢查GDB出 – Aaron

2

在您的scanf聲明中,您試圖將輸入值解釋爲double%lf)。相反,您應該以整數讀數,因爲您將所有變量聲明爲int。使用%d像這樣:

scanf("%d", &number_of_pennies); 

如果你還在迷茫,對printf formating specifications閱讀起來。

+0

我現在甚至覺得stupider,因爲我其實知道它的差異。非常感謝! – Aaron