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

In summary: Cannot assign to a named constant".In summary, the code won't compile because of a missing "then" statement.
  • #1
JamesWMH
3
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
  • #2


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
 
  • #3


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.
 
  • #4


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

Thanks

Jim
 
  • #5


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.
 
  • #6


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.
 
  • #7


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.
 
  • #8


suggestion of mark is absolutely correct

the error is coming due to absence of THEN statement

thanx
 
  • #9


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"
 

1. Why won't my program compile?

There could be several reasons why your program won't compile. Some common causes include syntax errors, missing or incorrect library files, or incompatible compiler versions.

2. How can I fix compiler errors?

The best way to fix compiler errors is to carefully read the error messages and identify the specific issue. Once you have pinpointed the problem, you can make the necessary changes to your code or configuration to resolve the error.

3. Why does my program compile on one computer but not another?

This could be due to differences in compiler versions, operating systems, or library dependencies. Make sure that all necessary components are present on the computer that is having trouble compiling the program.

4. Am I doing something wrong?

It is possible that you are making a mistake in your code that is causing the program to fail to compile. However, programming can be complex and it is not uncommon for even experienced programmers to encounter errors. It is important to carefully review your code and seek assistance if needed.

5. What can I do if I can't get my program to compile?

If you are unable to get your program to compile, it may be helpful to seek assistance from a more experienced programmer or consult online resources. You can also try breaking down your code into smaller sections and testing each one individually to identify the specific issue.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
25
Views
10K
  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
3
Views
10K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Back
Top