在输出中,new_dollars
始终显示美元数,因此我不知道如何解决该问题。请帮助我找出问题所在。
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", ¢s);
if ( cents >= 100 ){
cents = cents % 100;
new_dollars = dollars + cents / 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}
最佳答案
评论者对整数除法是正确的。尝试此操作,它交换执行计算的两行顺序,因此cents
在需要时仍将具有有用的值:
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", ¢s);
if ( cents >= 100 ){
new_dollars = dollars + cents / 100;
cents = cents % 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}