更改计数器c程序不显示正确的输出

时间:2014-10-11 02:36:50

标签: c

我正在制作一个改变计数器程序而且我被困住了,我在这里和其他2个编码论坛,google和youtube进行了搜索,但还没有找到答案...我的程序糟透了,我是全新的c / c ++ ...我的txt书还没到这里,所以我试着阅读每一件我能抓到的东西,直到它到达这里

这是输出应该在控制台窗口中的内容:

Welcome to Change Counter by Jo Mama!!
Please enter the total amount of purchase: $52.173
$52.173
Please enter amount of money tendered: $60
$60.00
Your change is: $7.83
-------------------------------------------
Twenties : 0
Tens : 0
Fives : 1
Ones : 2
Quarters : 3
Dimes : 0
Nickels : 1
Pennies : 3
-------------------------------------------
Thank you for using Change Counter!

这是我到目前为止所拥有的......

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


int main(void)
{
// define variables here
long double numberOfTwenties = 0;
long double numberOfTens = 0;
long double numberOfFives = 0;
long double numberOfOnes = 0;
long double numberOfQuarters = 0;
long double numberOfDimes = 0;
long double numberOfNickels = 0;
long double numberOfPennies = 0;
double purchasePrice = 0;
double amountTendered = 0;
double amountOfChange = 0;
double amountOfChangeCents = 0;

// start program here
printf("Welcome to the change counter by Josh Manion!!\n\n");
printf("Please enter the total amount of purchase: $");
scanf("%d", &purchasePrice);
printf("%d\n", purchasePrice);
printf("Please enter amount of money tendered: $");
scanf("%d", &amountTendered);
printf("%d\n", amountTendered);

//do change calculations here
amountOfChange = (amountTendered - purchasePrice);
printf("Your change is: $%d\n", amountOfChange);

numberOfTwenties = amountOfChange / 20;
//amountOfChange = numberOfTwenties %= amountOfChange;
numberOfTens = (amountOfChange / 10);
numberOfFives = (amountOfChange / 5);
numberOfOnes = (amountOfChange / 1);
numberOfQuarters = (amountOfChange * 0.25);

// print change calculations here


printf("---------------------------------------------\n");
//display denominations of change here
printf("Twenties: %d\n", numberOfTwenties);
printf("Tens: %d\n", numberOfTens);
printf("Fives: %d\n", numberOfFives);
printf("Ones: %d\n", numberOfOnes);
printf("Quarters: %d\n", numberOfQuarters);
printf("Dimes: %d\n", numberOfDimes);
printf("Nickels: %d\n", numberOfNickels);
printf("Pennies: %d\n", numberOfPennies);

printf("---------------------------------------------\n");
printf("Thank you for using the Change Counter!");

getchar();
return EXIT_SUCCESS;
}

问题是我的程序不起作用,它显示提示后的更改,但没有显示面额的细分,我倾向于使用“%”来降低更改金额,但我还没有找到任何示例。我不知道还有什么要说的......除了我是新人,这篇文章会帮助很多人......

1 个答案:

答案 0 :(得分:1)

您的程序有未定义的行为:您必须使用%Lf作为printf修饰符才能打印long double(使用scanf同样的问题)