#Having trouble getting them to run separately

25 messages · Page 1 of 1 (latest)

zealous hemlock
#

I know I am missing something that will make it so the code will run individually and not give me every answer at the same time.

hallow geodeBOT
#

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

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

zealous hemlock
#
{
    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;
}```
deft coral
#

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

fair lark
#

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 ..."

zealous hemlock
zealous hemlock
hallow geodeBOT
#

@zealous hemlock Has your question been resolved? If so, type !solved :)

zealous hemlock
#

Having trouble getting them to run separately

untold schooner
zealous hemlock
#

I was tyring to figure out how to just get the correct output isntead of all the possiblites

untold schooner
zealous hemlock
untold schooner
#

Theres is if, else if and else

#

Not if else

zealous hemlock
zealous hemlock
#

!solved