<分区>
我刚刚开始学习 C 编程,为大学的第一个类做准备。当我遇到问题时,我正在练习编写这段简单的代码。我不知道为什么,但是我放置代码的顺序改变了最终输出。这是我的代码:
#include "stdio.h"
/* A program to convert customer's coins into dollar slips */
#define DOLL 100 /* Conversion factor for dollar */
#define QURT 25 /* Conversion factor for quarter */
#define DIME 10 /* Conversion factor for dime */
#define NCKL 5 /* Conversion factor for nickel */
int main(void){
int dollar, quarter, dime, /* input - count of each coin types */
nickel, pennies; /* input - count of each coin types */
int total; /* compute - total of value of coins in cents */
int leftover; /* compute - leftover cents */
int total_dollar; /* compute - total $ value for credit slip */
char first, middle, last; /* input - 3 initials */
/* Get the count of each kind of coin */
printf("Number of $ coin> ");
scanf("%d",&dollar);
printf("Number of quarter> ");
scanf("%d",&quarter);
printf("Number of dimes> ");
scanf("%d",&dime);
printf("Number of nickels> ");
scanf("%d",&nickel);
printf("Number of pennies> ");
scanf("%d",&pennies);
/* Compute for the total value in cents */
total = dollar * DOLL + quarter * QURT + dime * DIME + nickel * NCKL + pennies;
/* Compute the final value of credit slip in dollar */
leftover = total % 100;
total_dollar = total / 100;
/* Get the initial for the slip */
printf("Enter your initial> ");
scanf("%c%c%c",&first, &middle, &last);
/* Output */
printf("Your credit slip>");
printf("%c%c%c credit",first, middle, last);
printf("\n%d $ and %d cents",total_dollar, leftover);
return (0);
}
当我先询问硬币数量然后询问初始值时,我得到了错误的结果(例如,如果我将 JRH 作为初始值,它最后只会返回 JR)。 但是如果我先得到用户的首字母,我就会得到想要的结果。任何人都可以解释一下吗?是否有我不知道的特定命令需要遵循?非常感谢!