I've just started with C programming to prepare for my first class in college. I was practicing writing this simple code when I encountered a problem. I don't know why but the order I put my code alter the final output. Here's my code:
#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);
}
When I ask for the coins count first and then the initial, I get the wrong result (e.g if I put JRH as initial, it'll only return JR in the end). But if I get the user's initial first, I get the desired result. Can anyone please explain this? Is there a specific order I need to follow that I'm unaware of? Thank you so much!