0
votes

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)
.....
2
Just a suggestion: You can cut your code sample down to just relevant methods. There's an awful lot there, and things like your tens array aren't relevant to the solution, so it's a pain to have to filter through that. - AdamMc331
double(3.75) - int(3.75) = 0.75 - Emadpres
@McAdam331: No, it's not "just a suggestion", actually: it's a requirement. We expect minimal examples on Stack Overflow! - Lightness Races in Orbit
Why are you using floating-point types for currency values? Simply do not do that. - Lightness Races in Orbit
@McAdam331: It does. And I don't blame you. Still, I think it's important to point out that this is a required step not an optional extra. - Lightness Races in Orbit

2 Answers

1
votes
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;
0
votes

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.