Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

FORTRAN Code Error - Need Help

  1. Mar 13, 2013 #1
    I am not able to get what's wrong in below.


    IF ( temp < 97.5 ) THEN
    WRITE (*, *) NTemperature below normalN
    ELSE IF ( temp > 97.5) THEN
    WRITE (*, *) NTemperature normalN
    ELSE IF ( temp > 99.5) THEN
    WRITE (*, *) NTemperature slightly highN
    ELSE IF ( temp > 103.0) THEN
    WRITE (*, *) NTemperature dangerously highN
    END IF

    Thanks.
     
  2. jcsd
  3. Mar 13, 2013 #2

    Borg

    User Avatar
    Gold Member

    The first two cases cover everything except when the temperature is exactly 97.5 so it can never reach the other cases where the temp is something else. Put the 103 case first followed by the 99.5 case.
     
  4. Mar 13, 2013 #3
    So it should be...the following code..am i right?

    IF ( temp > 103 ) THEN
    WRITE (*, *) NTemperature dangerously highN
    ELSE IF ( temp > 99.5) THEN
    WRITE (*, *) NTemperature slightly highN
    ELSE IF ( temp > 97.5) THEN
    WRITE (*, *) NTemperature normalN
    ELSE IF ( temp < 97.5) THEN
    WRITE (*, *) NTemperature belwo highN
    END IF
     
  5. Mar 13, 2013 #4

    Borg

    User Avatar
    Gold Member

    What do you think? Is it possible to reach every case with the right starting numbers? Try numbers that are equal to, just above, and just below each of your cases and see if it's possible to reach every case. The way that it's written, nothing will ever print if the temp is exactly 97.5. Can you think of how to fix that? The code is correct (except for the 97.5 problem) but you should always test like this.

    BTW, please use code blocks and indentation when posting code. This one is small enough but larger programs can be difficult to read otherwise. And, welcome to Physics Forums. :smile:
     
    Last edited: Mar 13, 2013
  6. Mar 13, 2013 #5
    thank you borg. I am learning about Fortran. I'm a rookie here.

    But your tip really helps.
     
  7. Mar 13, 2013 #6

    Borg

    User Avatar
    Gold Member

    Glad to help. I'm not a Fortran programmer but If/Else is universal in coding.

    Note: I'm only addressing the If/Else problem. Since I don't know Fortran, I have no idea if the WRITE statements are correct. They look odd to me but I guess that's how they're written?
     
  8. Mar 13, 2013 #7

    Mark44

    Staff: Mentor

    They need quotes around the text that is to be printed.

    For example, instead of this:
    Code (Text):
    WRITE (*, *) NTemperature dangerously highN
    do this:
    Code (Text):
    WRITE (*, *) "Temperature dangerously high"
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: FORTRAN Code Error - Need Help
  1. Help with fortran code (Replies: 17)

  2. Help with fortran code (Replies: 4)

Loading...