New to programming. Taking class in C. Below is my attempt at a program that will print "What is your hourly wage?" then read the dollar.cent amount and calculate salary. Then print the salary in the form, "Your total income over a year is X dollars and Y cents.
To isolate dollars from the dollar.cent amount, I truncated the value by converting to an int from a double. I wasn't sure how to get the cents, so I figured I could subtract the dollars from the dollars.cents (*100) and I'd have the number of cents.
I run the program and it works fine, except I don't get the amount of cents I expect.
If the user enters 18.33 as the hourly wage. Then I get 31826 for total Dollars, 31836.40 for total Income. But when I subtract them and multiply by 100, I am given 39 cents instead of 40.
int main(void) {
double totalIncome = 0.0;
int totalDollars = 0;
int totalCents = 0;
double hourlyWage = 0.0;
int hoursPerWeek = 40;
const int WEEKS_PER_YEAR = 52;
printf("What is your hourly wage? ");
scanf("%lf", &hourlyWage);
totalIncome = hourlyWage * hoursPerWeek * WEEKS_PER_YEAR;
totalDollars = totalIncome; //converts to int from double
totalCents = 10 * (totalIncome - totalDollars);
printf("Your total income over a year is %d dollars and %d cents", totalDollars, totalCents);
return 0;
}