# define vs constants












I had to do this program for a test, when i was using pound include define i was getting this error, so instead i used constants.

But i want to know why i am getting the error, i still could not figure it out, could someone tell me 1)why i am getting the error 2)how to fix it while still using #include define?



"Integral must have enup or num type"....










/*
Name Hasnain Attarwala
lab3 problem 7
Time Calculator p221
*/

#include<iostream>
#include<iomanip>

#define SEC_DAY 86400.0
#define SEC_HOUR 3600.0
#define SEC_MIN 60.0




using namespace std;

int main ()
{
int noOfSeconds, hours, mins, days, secs;
//const int SEC_DAY = 86400, SEC_HOUR = 3600, SEC_MIN = 60;

cout << "no of seconds" << endl;
cin >> noOfSeconds;

days = (noOfSeconds / SEC_DAY);
hours = (noOfSeconds % SEC_DAY) / SEC_HOUR;
mins = (noOfSeconds % SEC_DAY % SEC_HOUR) / SEC_MIN;
secs = (noOfSeconds % SEC_DAY % SEC_HOUR % SEC_MIN) ;

//a little bit of math

if (noOfSeconds >= SEC_DAY)
{

cout << endl << "The No of days are: " << fixed << setprecision(0)
<< days << endl;
cout << "The No of hours are: " << hours << endl;
cout << "The No of mins are: " << mins << endl;
cout << "The No of secs are: " << secs << endl << endl;
}


else if (noOfSeconds >= SEC_HOUR)
{
cout << endl << "The No of hours are: " << fixed << setprecision(0)
<< hours << endl;
cout << "The No of mins are: " << mins << endl;
cout << "The No of secs are: " << secs << endl << endl;
}


else if (noOfSeconds >= SEC_MIN)
{
cout << "The No of mins are: " << mins << endl;
cout << "The No of secs are: " << secs << endl << endl;
}

else
cout << "The No of secs are: " << secs << endl << endl;



return 0;
}



I don't follow your question.

#include define SOMENUMBER 5.0 is not legal

The way you have the defines in this post is.
Your problems are here - you can only use the modulus operator % on integer values.
1
2
3
hours = (noOfSeconds % SEC_DAY) / SEC_HOUR;
mins = (noOfSeconds % SEC_DAY % SEC_HOUR) / SEC_MIN;
secs = (noOfSeconds % SEC_DAY % SEC_HOUR % SEC_MIN) ;


Try making SEC_DAY etc integers, like this:

1
2
3
#define SEC_DAY 86400
#define SEC_HOUR 3600
#define SEC_MIN 60 
Topic archived. No new replies allowed.