-3

我是编程新手,但我正在编程课程中开始介绍它。问题是他们不教它,他们只是给我们做事。我们需要创建函数来根据一个人拥有的硬币和美元的数量(通过用户输入)来计算一个人拥有的美分数量。我不知道我在做什么,或者我什至是否正确使用了函数原型,但是当我运行我的程序时,我没有收到任何错误,但它只是打印了一个很大的负数。这是我所拥有的,我知道它很抱歉,但非常感谢任何帮助。

#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);
}
4

2 回答 2

3

阅读printf 格式说明符。正如 Guy Sirton 所说,当它们是不同的类型时,您的错误是使用(in )%lf读取。你应该使用for s。intscanf%dint

将来,您应该学习如何使用 GDB 等调试器,并使用它来逐步执行程序,并检查变量的值。

于 2013-02-08T01:31:14.110 回答
2

在您的scanf陈述中,您试图将输入值解释为double( %lf)。相反,您应该读取整数,因为您将所有变量声明为int. %d像这样使用:

scanf("%d", &number_of_pennies);

如果您仍然感到困惑,请阅读printf格式化规范

于 2013-02-08T01:32:32.897 回答