c++ - Modulus of an integer -


i'm writing program gets dollar amount input , displays amount in words. unable use strings, arrays, or functions @ point, i've worked out switch structures, though it's not efficient.

however problem comes when i've converted input float integer, , trying single out digits after decimal point using modulus; reason doesn't return correct digit.

so 321.78 extracting 1st decimal(or 8) returns 7. i'm not sure why happens, , how fix it.

here code first part

float number; int digit1, digit2, digit3, decimals, decimal1, decimal2, int_number;  cout << "enter dollar amount 0-1000: "; cin >> number;  int_number = number * 100;  digit1 = (int_number % 1000)/100; //ones digit2 = (int_number % 10000)/1000; //tens digit3 = (int_number % 100000)/10000; // hundreds  decimal1 = int_number % 10; decimal2 = (int_number % 100)/10; 

the decimal1 should return 8 if input 321.78. have missed something? appreciated.

it's converting 1.5 integer: result type doesn't have enough precision represent original value exactly. same thing 321.78: floating-point values cannot represent value exactly, , end value that's smaller expected, i.e., 321.77#### #### represents additional decimal digits tedious calculate here. when value multiplied 100 , converted int gets truncated, , 32177.#### becomes 32177.

edit: forgot mention solution.

int dollars; int cents; char dp; std::cin >> dollars; std::cin >> dp; std::cin >> cents; cents += dollars * 100; 

error checking left exercise reader.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -