If statements

I don't really know whats wrong with my code. I'm trying to get an output of:
45 seconds = 45 seconds
100 seconds = 1 minutes, 40 seconds
3800 seconds = 1 hours, 3 minutes, 20 seconds
88000 seconds = 1 days, 26 minutes, 40 seconds

But I'm just getting the days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream>
using namespace std;

int main()
{
	int seconds, minutes, hours, days, leftoverseconds, leftoverminutes, leftoverhours;
	int day = 86400, hour = 3600, minute = 60;
	
	cout << "Please enter a number of seconds. Do not enter negative numbers." << endl << endl;
	cin >> seconds;
	
	if (seconds > day)
	{
	days = seconds / day;
	leftoverhours = seconds % day;
	cout << endl << "The number in days is: " << days;
	
		if (leftoverhours >= hour && leftoverhours < day)
		{
		hours = leftoverhours / hour;
		leftoverminutes = leftoverhours % hour;
		cout << endl << "The number in hours is: " << hours;
		
			if (leftoverminutes >= minute && leftoverminutes < hour)
			{
			minutes = leftoverminutes / minute;
			leftoverseconds = leftoverminutes % minute;
			cout << endl << "The number in minutes is: " << minutes;
			cout << endl << "The number in seconds is: " << leftoverseconds << endl;
			}
		}
	}
return 0;	
}
You never have 'else' part
For example, here you are checking
if (leftoverhours >= hour && leftoverhours < day)
And if one of those conditions isn't met, your program doesn't go into the loop at all...
That's largely in part due to how you wrote your if statements. What if I entered 5 seconds? That's going to be less than your day value of 86400. And your program won't do anything with its nested if statements because the condition I entered never got past the first gate.

What you'll want to do is check for amounts AFTER you do your calculations.

For example: Let's say you entered 500 seconds (8 minutes, 20 seconds).

days = seconds/day => integer arithmetic would get you 0.

hours = leftOverHours/hour => after modulus operation, you'd still have 500/3600, which gets you 0.

minutes = leftOverMinutes/minute => from modulus, you get 500 % 60, which is 20, and then you have from a previous operation 500/60, which is 8 in integer arithmetic.

Finally, you want to check for whether or not the days, hours, and/or minutes are greater than 0. If they are, output them.
Last edited on
Your code only calculates the minutes if there are >0 days and hours, and only calculates the hours if there are >0 days. In my code the 'combination of all' line is what you want but the output is compressed into 1 line, I'm sure you could figure out how to write it in the format you wanted. You don't need lines 13-29 if all you want to display is a combination of all. And if that's what you want then it doesn't really make sense to use if statements at all.

Sorry the indentation is a little messed up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include <iostream>
using namespace std;

int main()
{
	int seconds, minutes, hours, days, days_leftover, hours_leftover, minutes_leftover;
	int day = 86400, hour = 3600, minute = 60;
	days = 0; // must initialize to 0 in case there's less than 1 day, in which case when the combination of all is displayed, days would be uninitialized
	
	cout << "Please enter a number of seconds. Do not enter negative numbers." << endl << endl;
	cin >> seconds;
	
	if( seconds > minute )
	{
	    minutes = seconds / minute;
	    cout << endl << "The number in minutes is: " << minutes;
	    
	    if( seconds > hour )
    	{
    	    hours = seconds / hour;
    	    cout << endl << "The number in hours is: " << hours;
    	    
    	    if( seconds > day )
        	{
        	    days = seconds / day;
        	    cout << endl << "The number in days is: " << days;
        	}
    	}
	}
	days_leftover = seconds % day;
	hours = days_leftover / hour;
	hours_leftover = days_leftover % hour;
	minutes = hours_leftover / minute;
	minutes_leftover = hours_leftover % minute;
	
	cout << endl << "The number as a combination of all is: " << days << " days, " << hours << " hours, " << minutes << " minutes, " << minutes_leftover << " seconds";
    
    return 0;	
}



Here is a test output in which only the last line matches what you wanted and the first 3 are if you do it in only either minutes,hours, or days.
1
2
3
4
5
6
7
8
Please enter a number of seconds. Do not enter negative numbers.

88000

The number in minutes is: 1466
The number in hours is: 24
The number in days is: 1
The number as a combination of all is: 1 days, 0 hours, 26 minutes, 40 seconds  
Last edited on
Topic archived. No new replies allowed.