Won't Compile the simplest of programs Am I being stupid?

  • Thread starter Thread starter JamesWMH
  • Start date Start date
  • Tags Tags
    Programs Stupid
Click For Summary

Discussion Overview

The discussion revolves around a Fortran 90 program that fails to compile, with participants exploring the reasons behind the compilation errors and suggesting corrections. The focus is on understanding the syntax and structure of conditional statements in Fortran, particularly in the context of error handling for negative inputs when calculating square roots.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion over an error message related to a "named constant" and the use of the "pause" statement, questioning its relevance to the compilation issue.
  • Another participant points out the need for an "end if" statement in the conditional structure and questions the purpose of the "stop" statement after the "if".
  • A participant clarifies that the "stop" statement is intended to prevent the program from executing a square root operation on negative numbers, but acknowledges that the logic could be improved.
  • There is a suggestion to modify the conditional check to ensure it correctly handles the case of zero and negative inputs.
  • Some participants emphasize the importance of including the "then" keyword in the "if" statement, noting that its absence may lead to compilation errors.
  • One participant mentions that the Fortran "if" statement can work without "then" when controlling a single statement, suggesting that the OP's code could have been valid if formatted correctly.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of proper syntax in Fortran, particularly regarding the use of "then" and "end if" in conditional statements. However, there are differing opinions on the handling of negative inputs and the necessity of the "stop" statement, indicating that the discussion remains unresolved on these points.

Contextual Notes

There are limitations regarding the assumptions made about the program's logic and the handling of negative numbers, as well as the specific error messages that arise from syntax issues. The discussion does not resolve these issues definitively.

JamesWMH
Messages
3
Reaction score
0
Won't Compile the simplest of programs! Am I being stupid?

Hi, I'm just getting my teeth back into Fortran 90 I had a course on it last year, and just want to familirise myself with it before I apply for post grad stuff.

Anyway, the program i have written is:

program emosix

real:: a, roota

write(*,*) 'Type an interger to find the square root (must be a positive number)'
read(*,*) a

if(a>=0.0)
stop

roota = sqrt(a)

write(*,*) 'The sqaure root of ', a, ' = ', roota

pause

end program emosix

and I get the error code:

Error: Cannot assign to a named constant at (1)
C:\Users\James\Documents\Programming\Emo6\~emo6.f90:15.5:

pause
1
Warning: Deleted feature: PAUSE statement at (1)

The warning about deleted feature hasn't made any difference in the past, so I don't think its that, but I have no idea what the cannot assign... part means?? Any ideas?

Thanks Jim
 
Technology news on Phys.org


Why is that stop in there after the if statement?

Also, your if statement needs an end if, I believe.
Code:
program emosix

real:: a, roota

write(*,*) 'Type an interger to find the square root (must be a positive number)'
read(*,*) a

if(a>=0.0) then
  roota = sqrt(a)
  write(*,*) 'The sqaure root of ', a, ' = ', roota

  pause
end if
stop
end program emosix
 


Hi

Thanks for the response, its in there to stop the program if the user puts a negative number in, as you can't have a square root of negative number.

I put in the end if and it gives me another error

if(a>=0.0)
1
Error: Cannot assign to a named constant at (1)
C:\Users\James\Documents\Programming\Emo6\~emo6.f90:11.3:

end if
1
Error: Expecting END PROGRAM statement at (1)
C:\Users\James\Documents\Programming\Emo6\~emo6.f90:17.5:

pause
1
Warning: Deleted feature: PAUSE statement at (1)

that is what I get now.
 


oops its supposed to be 'a<=0.0' but that didn't change anything.

Thanks

Jim
 


JamesWMH said:
Hi

Thanks for the response, its in there to stop the program if the user puts a negative number in, as you can't have a square root of negative number.
Well, you can, but the result won't be real. Instead of stopping the program, you should have logic that prevents the program from attempting to take the square root of a negative number.
JamesWMH said:
I put in the end if and it gives me another error

if(a>=0.0)
1
Error: Cannot assign to a named constant at (1)
C:\Users\James\Documents\Programming\Emo6\~emo6.f90:11.3:

end if
1
Error: Expecting END PROGRAM statement at (1)
C:\Users\James\Documents\Programming\Emo6\~emo6.f90:17.5:

pause
1
Warning: Deleted feature: PAUSE statement at (1)

that is what I get now.

JamesWMH said:
oops its supposed to be 'a<=0.0' but that didn't change anything.
You can take the square root of 0, so you should make the test expression a < 0.0.

As far as the "can't assign to a named constant" business, try using a different variable for roota.

What does your code look like now? You if statement wasn't formed correctly before, and it still might not be right.
 


JamesWMH said:
if(a>=0.0)

You need a "then". For example (also fixing your test condition):

Code:
      if (a <= 0.0) then
         stop
      end if

For some reason, the missing "then" triggers the "can't assign to a named constant" error.
 


jtbell said:
You need a "then".
I added one in the code I showed in post #2, but didn't say that explicitly. I should have, since the OP seems to have missed it.
jtbell said:
For example (also fixing your test condition):

Code:
      if (a <= 0.0) then
         stop
      end if

For some reason, the missing "then" triggers the "can't assign to a named constant" error.

The condition can be written as a < 0.0.
 


suggestion of mark is absolutely correct

the error is coming due to absence of THEN statement

thanx
 


I know this is an old thread that's been bumped for some reason, but I would like to add one more thing.

The Fortran "if" statement doesn't require a "then" (or an "endif" either) when the "if" is only controlling a single statement.

In this sense the OP's code should (almost) have worked correctly, had he not put the "new line" after the "if" statement (since end of line is more than just white space in Fortran source code).

Code:
   if (a <= 0.0) stop

This should have worked correctly without "then" and without "endif"
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 25 ·
Replies
25
Views
10K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
6
Views
4K
Replies
13
Views
3K