error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’

Hi,

I’m trying to use the modulus operator in my coding, and every time I compile it the message "error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’ " comes up.

This is my code:

#include
#include
using namespace std;

int main()
{

double seconds, re_seconds, minutes, re_minutes, hours, re_hours, days, re_days, weeks;

cout<<“Hello this program calculates time from seconds.”<<endl;
cout<<endl;

cout<< “Enter a number of seconds.”<<endl;
cin>>seconds;

// Calculate the minutes, hours, days, weeks

re_seconds=seconds% 60;
minutes=seconds/60;
re_minutes=seconds% 3600;
hours=seconds/3600;
re_hours=seconds% 86400;
days=seconds/86400;
re_days=seconds% 604800;
weeks=seconds/604800;

// Convert from seconds to minutes, hours, and days

if(seconds>0 && seconds<60)
{
cout<<seconds<<" seconds = “<<seconds<<” seconds"<<endl;
}
else if(seconds>=60 && seconds<3600)
{
re_seconds=seconds% 60;
minutes=seconds/60;
cout<<seconds<<" seconds = “<<minutes<<” minute(s), “<<re_seconds<<” seconds"<<endl;
}
else if(seconds>=3600 && seconds<86400)
{
re_minutes=seconds% 3600;
hours=seconds/3600;
cout<<seconds<<" seconds = “<<hours<<” hour(s), “<<re_minutes<<” minutes"<<endl;
}
else if(seconds>=86400 && seconds<604800)
{
days=seconds/86400;
cout<<seconds<<" seconds = “<<days<<” day(s)"<<endl;
}
else if(seconds>=604800)
{
weeks=seconds/604800;
cout<<seconds<<" seconds = “<<weeks<<” week(s)"<<endl;
}
else if(seconds<=0)
{
cout<<“Input is invalid. Put in a different value in the next run.”<<endl;
}

return 0;

}

Is there any way it can be fixed?

Hw2_q3_code.cpp (1.54 KB)

antione96: Hi,

I'm trying to use the modulus operator in my coding, and every time I compile it the message "error: invalid operands of types ‘double’ and ‘int’ to binary ‘operator%’ " comes up.

This is the part of my code where the error comes up:

re_seconds=seconds%60; re_minutes=seconds%3600;

Is there any way it can be fixed?

Yes, but show your entire code (or a compilable subset that demonstrates the error.) Please put your code in [ code ] tags so we can see it undisturbed.

you might try to cast numbers to a variable, something like this

double Sixty = 60;

re_seconds = seconds % Sixty;

ZBay:
you might try to cast numbers to a variable, something like this

double Sixty = 60;

re_seconds = seconds % Sixty;

I tried that and it still does not work.

what compiler are you using?

Linux

alright here is what I figured the cmath uses fmod(numerator, denominator) to calculate the remainder. I initialized the variables a different way not sure if that is needed but as it’s written below the code compiles on g++

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

   double seconds = 0;
   double re_seconds = 0;
   double minutes = 0;
   double re_minutes = 0;
   double hours = 0;
   double re_hours = 0;
   double days = 0;
   double re_days = 0;
   double weeks = 0;
   double Sixty = 60;
   
   
   cout <<"Hello this program calculates time from seconds." <<endl;
   cout << endl;

   cout << "Enter a number of seconds." <<endl;
          cin >> seconds;

   // Calculate the minutes, hours, days, weeks
   
   re_seconds = fmod (seconds, Sixty);
   minutes=seconds/Sixty;
   re_minutes=fmod(seconds,3600);
   hours=seconds/3600;
   re_hours=fmod(seconds,86400);
   days=seconds/86400;
   re_days=fmod(seconds, 604800);
   weeks=seconds/604800;

   // Convert from seconds to minutes, hours, and days

   if(seconds>0 && seconds<60)
   {
      cout<<seconds<<" seconds = "<<seconds<<" seconds"<<endl;
   }
   else if(seconds>=60 && seconds<3600)
   {
      re_seconds=fmod(seconds, 60);
      minutes=seconds/60;
      cout<<seconds<<" seconds = "<<minutes<<" minute(s), "<<re_seconds<<" seconds"<<endl;
   }
   else if(seconds>=3600 && seconds<86400)
   {
      re_minutes=fmod(seconds, 3600);
      hours=seconds/3600;
      cout<<seconds<<" seconds = "<<hours<<" hour(s), "<<re_minutes<<" minutes"<<endl;
   }   
   else if(seconds>=86400 && seconds<604800)
   {
      days=seconds/86400;
      cout<<seconds<<" seconds = "<<days<<" day(s)"<<endl;
   }
   else if(seconds>=604800)
   {
      weeks=seconds/604800;
      cout<<seconds<<" seconds = "<<weeks<<" week(s)"<<endl;
   }
   else if(seconds<=0)
   {
      cout<<"Input is invalid. Put in a different value in the next run."<<endl;
   }

        return 0;

}

% is an int operator. It does not work with float.

Regards, Ray L.