I am converting seconds to minutes, hours, days. For some reason I have an issue with the days not calculating when running the program. The 'if' statement seems completely ignored by the program. I think this may be caused by the warning I get. Any help would be greatly appreciated in explaining the warning and my current issue:

Warning: Constant is long in function main()

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	int seconds, minute, hour;
	double day;

	cout << "Enter the number of seconds: ";
	cin >> seconds;

	day = seconds / 86400;
	seconds = seconds % 86400;
	hour = seconds / 3600;
	hour = hour % 3600;
	minute = seconds / 60;
	minute = minute % 60;
	seconds = seconds % 60;

		if (day > 0)
		{
			cout << "Number of days: " << day << endl;
		}
		if (hour > 0)
		{
			cout << "Number of hours: " << hour << endl;
		}
		if (minute > 0)
		{
			cout << "Number of minutes: " << minute << endl;
		}
		if (seconds > 0)
		{
			cout << "Number of seconds: " << seconds << endl;
		}
	return 0;
}
13. day = seconds / 86400;
14. seconds = seconds % 86400;
15. hour = seconds / 3600;
16. hour = hour % 3600;
17. minute = seconds / 60;
18. minute = minute % 60;
19. seconds = seconds % 60;

You're overwriting your hour and minute answers immediately after generating them. Lines 16 and 18 are wrong.

Edited 5 Years Ago by Fbody: n/a

Could you give insight as to how you would arrange the lines? Just removing the lines removes the mathematical process of converting seconds into minutes / hours, causing math errors when the program is ran. Also removing the lines does not fix the issue of 'days' not being computed.

You need to think about what you are doing and look carefully at the variables you are using. Are they the correct variables for each line?

I have double checked and can not see where I have made my mistake. Could you explain further ?

You need to think about what you are doing and look carefully at the variables you are using. Are they the correct variables for each line?

Thank you WaltP, I've changed the variables some. I've changed the constants to long and I have added 'L' to 'days = seconds / 86400L;'. The warnings are gone and days are only computed some of the time. For example I can do 86401 seconds, but not 172800 (2 days). Any new knowledge is again appreciated. Code below.

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	long minute, hour, day;

	cout << "Enter the number of seconds: ";
	long seconds;	//variable definition
	cin >> seconds;

	day = seconds / 86400L;
	hour = seconds / 3600;
	hour = hour % 3600;
	minute = seconds / 60;
	minute = minute % 60;
	seconds = seconds % 60;

	// Calculate conversion
		if (day > 0L)
		{
			cout << "Number of days: " << day << endl;
		}
		if (hour > 0)
		{
			cout << "Number of hours: " << hour << endl;
		}
		if (minute > 0)
		{
			cout << "Number of minutes: " << minute << endl;
		}
		if (seconds > 0)
		{
			cout << "Number of seconds: " << seconds << endl;
		}
	return 0;
}

Why don't you actually look at the values as you calculate them -- just to see if your calculations are correct. Output them. I assume you know what DD:HH:MM:SS you are expecting from the seconds you enter, right?

Also, it might help to actually use pencil and paper to execute your code by hand. That's the best debugging you can do at this stage.

Edited 5 Years Ago by WaltP: n/a

Why don't you actually look at the values as you calculate them -- just to see if your calculations are correct. Output them. I assume you know what DD:HH:MM:SS you are expecting from the seconds you enter, right?

Also, it might help to actually use pencil and paper to execute your code by hand. That's the best debugging you can do at this stage.

I feel like I am moving in circles redoing the math and code, so I have tried to make it even simpler, however none of the things I'm doing (completely rewritten the code) makes the days show up when running the program.

I'll sleep it on it, thanks ahead of time WaltP.

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	long minute, hour, day;

	cout << "Enter the number of seconds: ";
	int seconds;	//variable definition
	cin >> seconds;

	day = seconds / 60 / 60 / 24;
	hour = (seconds / 60 / 60) % 24;
	minute =(seconds / 60) % 60;
	seconds = seconds % 60;

	// Calculate conversion
		if (day > 0)
		{
			cout << endl << "Number of days: " << day << endl;
		}
		if (hour > 0)
		{
			cout << "Number of hours: " << hour << endl;
		}
		if (minute > 0)
		{
			cout << "Number of minutes: " << minute << endl;
		}
		if (seconds > 0)
		{
			cout << "Number of seconds: " << seconds << endl;
		}
	return 0;
}

i think this should work.....

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	int seconds, minute, hour;
	double day;

	cout << "Enter the number of seconds: ";
	cin >> seconds;
	
	minute=seconds/60;
	
	if(minute!=0)
		seconds = seconds%(minute*60);
	else
		seconds=0;
		
	hour = minutes/60;
	if(hour!=0)
		minute = minute%(hour*60);
	else
		minute=0;
	
	day=hour/24;
	if(day!=0)
		hour=hour%(day*24);
	else
		hour=0;
	

		if (day > 0)
		{
			cout << "Number of days: " << day << endl;
		}
		if (hour > 0)
		{
			cout << "Number of hours: " << hour << endl;
		}
		if (minute > 0)
		{
			cout << "Number of minutes: " << minute << endl;
		}
		if (seconds > 0)
		{
			cout << "Number of seconds: " << seconds << endl;
		}
	return 0;
}

if you want me to explain further let me know.....
otherwise... please mark the thread as solved.....

Comments
Why are you solving the program for him? I could have done that!!!

You were closer before.

@WaltP You're being very convoluted. I understand you're not obligated to help, but your replies are very obscuring. If you can point out the obvious, I'd just appreciate that more. Thanks for your previous replies.

@Caligulaminus Did the days show for you? For some reason my code is skipping over the entire

// Calculate conversion
		if (day > 0)
		{
			cout << endl << "Number of days: " << day << endl;
		}

Any ideas? Otherwise thanks for the reply.

@arjunpk You've got 'minutes' as one variable (instead of minute) and your output doesn't show hours or days. I copied directly from your post. Perhaps I'm missing something all together to show days. Thanks in advance.

Edited 5 Years Ago by Skabix: Erroneous sp.

Skabix...
why don't you eliminate the if statements and see what the output is......
i did not have a c++ compiler so i couldn't verify the code.......
anyways....
tell me what your input is......
and without the if statements....
just try printing the output......

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	int seconds, minute, hour;
	double day;

	cout << "Enter the number of seconds: ";
	cin >> seconds;
	
	minute=seconds/60;
	
	if(minute!=0)
		seconds = seconds%(minute*60);
	else
		seconds=0;
		
	hour = minute/60;
	if(hour!=0)
		minute = minute%(hour*60);
	else
		minute=0;
	
	day=hour/24;
	if(day!=0)
		hour=hour%(day*24);
	else
		hour=0;
	

		cout << "Number of days: " << day << endl;
		cout << "Number of hours: " << hour << endl;
		cout << "Number of minutes: " << minute << endl;
		cout << "Number of seconds: " << seconds << endl;
		
	return 0;
}

check what the output is for this......
and also tell us what your input is...

and i still don't understand why you have day as double.......
try keeping day as int....
either keep day as long or int itself.....
and when you are using 2 different data types.....
try typecasting it.....

@WaltP You're being very convoluted. I understand you're not obligated to help, but your replies are very obscuring. If you can point out the obvious, I'd just appreciate that more. Thanks for your previous replies.

Sorry. I was trying to get you to think through your problem, and try to debug what you started with. Since you didn't bother to try any of my suggestions, I'll back out.

Good luck.

@arjunpk I am unsure of what typecasting is but your output again did not show up after removing if statements. I keep switching from int to long to see if it was a conversion issue, but the issue on the whole was 'days' was not showing up, not my math. I've rewritten the code and got it to work, however I am unsure why it wouldn't before.

Sorry. I was trying to get you to think through your problem, and try to debug what you started with. Since you didn't bother to try any of my suggestions, I'll back out.

Good luck.

I have thought about this problem for 2 days before posting here, so trust me I've tried debugging and thinking my way through. I decided to post because I needed an experienced person's help, however I understand these forums have a majority "do your own homework" mentality. I first off didn't understand your suggestions, and secondly didn't get more feedback than "keep at it" approach. Nonetheless I understand programming is more debugging than inventing, so nonetheless you have taught me something of great value and I appreciate it.

Final Code.

#include <iostream.h>
//using namespace std;

int main()
{
	// Constants for Conversion
	long seconds, minutes, hours, days;

	cout << "Enter the number of seconds: ";
	cin >> seconds;

	day = (seconds / 60 / 60) / 24;
	hours = (seconds / 60 / 60) % 24;
	minutes =(seconds / 60) % 60;
	seconds = seconds % 60;

	// Calculate conversion
		 if (days > 0)
		 {
			cout << "Number of days: " << days << endl;
		 }
		if (hours > 0)
		{
			cout << "Number of hours: " << hours << endl;
		}
		if (minutes > 0)
		{
			cout << "Number of minutes: " << minutes << endl;
		}
		if (seconds > 0)
		{
			cout << "Number of seconds: " << seconds << endl;
		}
	return 0;
}
This question has already been answered. Start a new discussion instead.