• Post Reply Bookmark Topic Watch Topic
  • New Topic

if statement help  RSS feed

 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

i'm having a problem with this program not working as I intended.

So the program works, too well actually, when the seconds entered go past 3600 or 86400 the program calculates the mins, hours and seconds. I am trying to make it so if the seconds are 90000 it will just give me the days. or if its 7000 seconds then it will calculate the hours.

thank you

 
Nimit Bhargava
Greenhorn
Posts: 6
C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Martinez wrote:


Try this intead of the above code snippet:
 
Adrian Martinez
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that worked, and it helped me understand what I was doing incorrectly.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nimit Bhargava wrote: . . .
Although that code accurately reflects its structure, you should add {} and it is usual to indent else‑ifs differently, so it comes out as:-Whenever you use a block of else‑ifs, make sure they run in order.
More style things: don't expect people to know what 60 or 86400 mean: use constants: SECONDS_IN_DAY or SECONDS_IN_MINUTES. And never use the tab key.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I most cases where you use multiple if‑elses, it is worth ending the series with a plain simple else to catch any possibilities not yet covered. In some cases the code won't compile otherwise.
 
Nimit Bhargava
Greenhorn
Posts: 6
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestion, Campbell.
Will follow that from now.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. I've even written something like


Then if it DOES happen, I know I have some logic wrong somewhere...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!