Need Help with some simple code

I am going back through my textbook to get better and programming and I am getting this code error.

1>c:\documents and settings\acaughg\my documents\visual studio 2008\projects\ch2pe16\ch2pe16\main.cpp(21) : error C2296: '%' : illegal, left operand has type 'double'
1>c:\documents and settings\acaughg\my documents\visual studio 2008\projects\ch2pe16\ch2pe16\main.cpp(23) : error C2296: '%' : illegal, left operand has type 'double'

Here is the program.

#include<iostream>

using namespace std;

int main()
{

double centimeters;
double totalInches;

double yard;
double feet;
double inches;

cout<<"Input length in centimeters: "<<endl;
cin>>centimeters;

totalInches = centimeters/2.54;

yard = totalInches/36;
totalInches = totalInches%36;
feet = totalInches/12;
totalInches = totalInches%12;
totalInches = inches;

cout<<"The total length is "<<yard<<" yard(s), "<<feet<<" feet (foot), and "<<inches<<" inch(es)."<<endl;

system("pause");

}

I know the error is with ->totalInches = totalInches%36; but I dont understand why. I used the same code on another program and the only difference was I was using int data types and now I am using double. Any help would be appreciated.
% gives you the remainder of an integer division. So it only works with integers.
I don't think you can use the modulus operator with floating points... though I'm new to C++ so that might be incorrect. You could try doing the static_cast<int> for those particular lines.
It makes no sense with floating point; the modulus needs the remainder after division, but in floating point division, there's no remainder.
I guess I will rewrite using static_cast. Thanks all
Final Program:

#include<iostream>

using namespace std;

int main()
{

double centimeters;

int totalInches;

int yard;
int feet;
int inches;

cout<<"Input length in centimeters: "<<endl;
cin>>centimeters;

totalInches = static_cast<int>(centimeters/2.54);

yard = totalInches/36;
totalInches = totalInches%36;
feet = totalInches/12;
totalInches = totalInches%12;
inches = totalInches;

cout<<"The total length is "<<yard<<" yard(s), "<<feet<<" feet (foot), and "<<inches<<" inch(es)."<<endl;

system("pause");

}
Topic archived. No new replies allowed.