In this code I have defined the remainder in this code as float data type but the compiler rejected that and suppose to defien it as an inegral type what is the reasone for this issue?
enter image description here
int main()
{
float days, hours, minutes, seconds;
float Totalseconds;
float Remainder;
float secondsperday;
float secondsperhour;
float secondsperminute;
secondsperday = 24 * 60 * 60;
secondsperhour = 60 * 60;
secondsperminute = 60;
cout << "Enter total seconds\n";
cin >> Totalseconds;
days = floor(Totalseconds / secondsperday);
Remainder = (Totalseconds % secondsperday);
hours = floor(Remainder / secondsperhour);
Remainder = Remainder % secondsperhour;
minutes = floor(Remainder / secondsperminute);
Remainder = Remainder % secondsperminute;
seconds = Remainder;
cout << days << ":" << hours << ":" << minutes << ":" << seconds;
return 0;
}
>Solution :
expression must have integral or unscoped enum type on %
Floating-point numbers like float
don’t have a %
operator in C++.
They are neither integral (i.e. integers like int
) nor unscoped enumerations (i.e. enum
), so you’re getting this error.
If you wanted a floating-point remainder that works similarly to %
operator, you could use std::fmod
:
Remainder = std::fmod(Totalseconds, secondsperday);
However, in your code, it looks like none of the numbers have to be float
(unless Totalseconds
can be 123.456 for example).
If you simply changed all of the float
types to int
, this code would also compile.