0

I am a beginner in C++ and for an assignment I wrote code for a number to English conversion. My problem is getting the decimal from an integer. Upon advice I changed the void expand to a double value and changed the code for getting the decimal, and now I receive an "invalid operands of types double and int to binary operator %" for the remainder of my code. Something to do with (value)?

void expand(double);

int main()

{
 ......
}
void expand(double value)
{
string const ones[20] = 
    { 
     " ...... "
    }           
if(value>1)
    {
    double decimalPart = value - (int)value;
    }
else if(value>=1000)
{
    expand(value/1000);
    cout<<" thousand";
    if(value % 1000)
.....
JUTG
  • 1
  • 2

2 Answers2

1
void expand(int value)

Change the above to double for anything to work

void expand(double value)

Also you can get the decimal part alone by the following after changing the above

double decimalPart = value - (int)value;
Titus
  • 907
  • 8
  • 18
  • I changed the datatypes and now the remainder of my code is giving me "invalid operands of types double and int to binary operator %". – JUTG Oct 25 '14 at 21:40
0

value is an int so any arithmetic operation done using it will disregard the decimal part. So what you are doing when passing num into the expand function is casting it as an int. Hence you are removing the decimals.

double foo = 123.45;
expand(foo); // This will take only 123 and disregard the .45

Declare it as a double in void expand(double value) and see what happens.

JoErNanO
  • 2,458
  • 1
  • 25
  • 27