Question

I have a program that is supposed to tell a user if a temperature they entered is absolute zero. If indeed it is, then they should see "no entropy for you", if it is greater, then It should tell them how far above a0 they are, but i keep getting messages saying my if statements have errors, and have no idea where to start, can anyone help me out here?

PROGRAM Project2

CHARACTER(1):: tempType
REAL:: k, f, c
REAL:: temp
REAL:: answer

PRINT *, "What is the temperature type?"
READ *, tempType
PRINT *, "whats the temp?"
READ *, temp

k = 0.0
c = -273.15
f = -459.67

answer = getMinTemperature(tempType)
PRINT *, answer


CONTAINS

FUNCTION getMinTemperature(tempType)
REAL:: getMinTemperature
CHARACTER(1), INTENT(IN):: tempType
REAL:: temp
DO i = 1, num
IF(ACHAR(tempType(i)) <= temp .AND. k > temp) THEN
 k= 0.0
 getMinTemperature = k
 EXIT
ELSE IF (c <= temp .AND. c > temp) THEN
 c= -273.15
 getMinTemperature = c
 EXIT
ELSE IF ( f <= temp .AND. f > temp) THEN
 f=-459.67
 getMinTemperature = f
 EXIT
END IF
END DO

END FUNCTION

END PROGRAM Project2
Was it helpful?

Solution 2

Assuming it's a homework, I'll just give you a couple of pointers rather than the answer. First of all, reconsider the logic of your program. Do you really need that loop over i? Then, look up the first google hit for achar. Do you really need it? Also notice that your code compiles if you comment out the line with achar, so that it is the latter which interferes with the intent(in) attribute of the function argument.

OTHER TIPS

Here's some gratuitous advice, only tangentially related to the errors you report.

  1. Get into the habit of inserting implicit none at the start of your programs, routines and modules. It should go after any use statements, before any other declarations. It will help you to avoid accidental declaration of variables, or re-declaration of variables in scopes where you don't want them redeclared.
  2. Get into the habit of passing all data in or out of a routine through its argument list. In your code the variables k,f,c (what is this, part of a chicken-frying code ?) are within scope of the contained getMinTemperature so can be used there without passing them explicitly. But your current approach makes it much harder, in large programs, to figure out what's going on and will make it much more difficult to re-use code in other programs.

Zhenya is absolutely correct. Just to give you a bit more of a hint, you might want to use the following code sample in your function. You will of course have to add in the needed logic. Your assignment lends itself well to using the forSELECT CASE

SELECT CASE ( tempType )
 CASE ( 'k', 'K' )
     ! do something - kelvin input
 CASE ( 'c', 'C' )
     ! do something - celsius input
 CASE ( 'f', 'F' )
     ! do something - fahrenheit input
 CASE DEFAULT
     ! handle input error
END SELECT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top