#Having trouble getting them to run separately
25 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
@zealous hemlock
Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!
{
double seconds, minutes, hours;
printf("Please input whole number of seconds:\n");
scanf("%lf", &seconds);
hours =floor(seconds / 3600);
minutes =floor(fmod(seconds,3600)/60);
seconds = fmod(seconds, 60);
if (minutes && seconds==0)
printf("%lf hour \n", hours);
if (hours && seconds>0 )
printf("%lf hours %lf seconds \n", hours, seconds);
if (hours && minutes>0)
printf("%lf hours %lf minutes \n", hours, minutes);
printf("%lf hours %lf minutes %lf seconds \n", hours, minutes, seconds);
return 0;
}```
do you know about else?
Also, minutes && seconds == 0 (and the rest of the conditions) doesn't do what you think it does; it is equivalent to minutes != 0 && seconds == 0
Your code isn't nested.
if (hours && minutes>0)
printf("%lf hours %lf minutes \n", hours, minutes);
printf("%lf hours %lf minutes %lf seconds \n", hours, minutes, seconds);
Will print without evaluating the conditions.
int main()
{
double seconds, minutes, hours;
printf("Please input whole number of seconds:\n");
scanf("%lf", &seconds);
hours =floor(seconds / 3600);
minutes =floor(fmod(seconds,3600)/60);
seconds = fmod(seconds, 60);
if (minutes && seconds==0)
printf("%lf hour \n", hours);
if (hours && seconds>0 )
printf("%lf hours %lf seconds \n", hours, seconds);
if (hours && minutes>0)
printf("%lf hours %lf minutes \n", hours, minutes);
else
printf("%lf hours %lf minutes %lf seconds \n", hours, minutes, seconds);
return 0;
}
Should work.
But prefer using curly braces to avoid confusion. Like it :
if (hours && seconds > 0)
{
printf("%lf hours %lf minutes \n", hours, minutes);
}
Mind that if (minutes && secondes==0) isn't the same as if (minutes == 0 && seconds == 0)
In C, the condition if (var) is true if var is different from 0 and false otherwise. So if (minutes && seconds == 0) means "if minutes is different from 0 and seconds is equal to 0, do ..."
yes, i think im supposed to use them but I couldnt get it to work while using else so i thought i could just work around it x_x
o I see, so it should be more like minutes !=0 && seconds!=0
that makes sense, ty
@zealous hemlock Has your question been resolved? If so, type !solved :)
Having trouble getting them to run separately
What exactly was the issue, if you remember?
when i run the code, lets say I input "12876 seconds" and expect output "3 hours 34 minutes 36 seconds", I will get
"3 hours
3 hours 36 seconds
3 hours 34 minutes
3 hours 34 minutes 36 seconds "
I was tyring to figure out how to just get the correct output isntead of all the possiblites
Do you know about this:
if () {
} else if () {
} else {
}
You can add as many else if as you want and check for all different possibilities. Even leave the closing else, if you don't need it anymore^^
yes but when i try to use else
{
printf("%0.lf hour \n", hours);
}
if else (hours>1)
{
printf("%0.lf hours \n", hours);
}
i get
Yeah, bc you wrote if else.
Theres is if, else if and else
Not if else
ah i fixed it, thank you, I will keep messing around with it!
!solved