C++ Object-Oriented Programming - I need someone to explain this line of code to
ID: 3878273 • Letter: C
Question
C++ Object-Oriented Programming - I need someone to explain this line of code to me and also tell me how I can eliminate the 'warning' I'm getting from my compiler that is saying narrowing conversion. 'Money' is a struct that has two variables that are both type int, interest is declared and defined as a double. 'account.interest_rates' is a double, but 'account.amount.dollars' and before the type cast 'account.amount.cents' are both int. I just need to know how to get that warning to go away for the narrowing conversion or a better way so that the calculations still don't get messed up.
double interest = account.interest_rates * (account.amount.dollars + (double) account.amount.cents/100) + 0.005;
Money interest_money = {(int)interest, (interest - (int)interest)*100};
Explanation / Answer
double interest = account.interest_rates * (account.amount.dollars + (double) account.amount.cents/100) + 0.005;
The above lines are fine, it means account interest rate is multiplied by amount + cents .
<Warning is because of below line>, Money struct as int, You are trying to narrow down from double to int . here there is warning because integer is lower than double in terms of size
Use below line to fix the issue
Money interest_money = {(int)interest, (int)(interest - (int)interest)*100};
Thanks, let me know if there is any concern. PLEASE RATE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.