Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ expression must have integral or unscoped enum type on %

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading