Need help with Fortran homework -- Error: Invalid form of PROGRAM statement

Click For Summary

Discussion Overview

The discussion centers around a Fortran programming homework problem, specifically addressing an error related to the PROGRAM statement and issues with string handling in the code. Participants explore potential solutions and troubleshooting steps.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error message indicating an "Invalid form of PROGRAM statement," but the exact line causing the issue is unclear.
  • Another participant suggests recompiling the code after saving it to see if the error persists, noting a discrepancy between the error message and the provided code.
  • A different participant recommends removing spaces from the label in the format statement, citing that older Fortran versions may have strict column requirements.
  • There is a suggestion to eliminate the format statement entirely and use a simpler read statement instead.
  • One participant questions whether an entire array can be printed in the manner shown, suggesting that older Fortran versions may require looping through elements for printing.

Areas of Agreement / Disagreement

Participants express differing views on the source of the error and potential solutions, indicating that no consensus has been reached regarding the best approach to resolve the issue.

Contextual Notes

Participants note that older versions of Fortran may have specific formatting requirements that could affect code execution, but the exact implications of these requirements remain unresolved.

suezxc6
Messages
11
Reaction score
0
Homework Statement
im trying to compile this program but i get this error

print*, cap= one (m:m)------------------------------Capitalize:
1
Error: Invalid form of PROGRAM statement at (1)
Relevant Equations
,
Fortran:
program Lab5A
      implicit none
      ! This program introduces Fortran string handling capabilities

      character*26 upper, lower, name, cap
      character str*2, one*1
      integer from, to, i, m

      lower = "abcdefghijklmnopqrstuvwxyz"
      upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      name = "loool"

      ! ---------------------------------------------------Substrings:
      do i = 1, 5
         print*, "The string we are studying is: ", name
         print*, "Enter two integers ..."
         read*, from, to
         print*, name(from:to)
      end do

      ! ---------------------------------------------------Pattern:
      do i = 1, 5
         print*, "Enter any 2-character string (e.g. enter er) ..."
         read 5, str
         print*, index(name,str)
      end do
    5 format(A)

      ! ---------------------------------------------------Capitalize:
      do i = 1, len(name)
         one = name(i:i)
         m = index(lower,one)
         if (m .ne. 0) then
            one = upper (m:m)
         end if
         cap(i:i) = one
       end do
       print*, cap
   
       end
 
Last edited by a moderator:
Physics news on Phys.org
suezxc6 said:
Homework Statement:: I am trying to compile this program but i get this error

print*, cap= one (m:m)------------------------------Capitalize:
1
Error: Invalid form of PROGRAM statement at (1)
Relevant Equations:: ,

Fortran:
program Lab5A
      implicit none
      ! This program introduces Fortran string handling capabilities

      character*26 upper, lower, name, cap
      character str*2, one*1
      integer from, to, i, m

      lower = "abcdefghijklmnopqrstuvwxyz"
      upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      name = "loool"

      ! ---------------------------------------------------Substrings:
      do i = 1, 5
         print*, "The string we are studying is: ", name
         print*, "Enter two integers ..."
         read*, from, to
         print*, name(from:to)
      end do

      ! ---------------------------------------------------Pattern:
      do i = 1, 5
         print*, "Enter any 2-character string (e.g. enter er) ..."
         read 5, str
         print*, index(name,str)
      end do
    5 format(A)

      ! ---------------------------------------------------Capitalize:
      do i = 1, len(name)
         one = name(i:i)
         m = index(lower,one)
         if (m .ne. 0) then
            one = upper (m:m)
         end if
         cap(i:i) = one
       end do
       print*, cap
 
       end
The error you show doesn't agree with the code you listed. The compiler is complaining about this line print*, cap= one (m:m)------------------------------Capitalize:
but there's no line that looks like that in your code. My suggestion is to save your code, and then recompile, and see if you get the same error again.
 
  • Like
Likes   Reactions: sysprog
i did just as you requested and still got this
 

Attachments

  • s.png
    s.png
    8.6 KB · Views: 285
Last edited by a moderator:
Here's what I would try -- remove all spaces from the label 5 in your format statement.
Fortran:
    do i = 1, 5
        print*, "Enter any 2-character string (e.g. enter er) ..."
        read 5, str 
        print*, index(name,str)
    end do 
5 format(A)
Older Fortran versions were very picky about what could appear in a given column. Perhaps your compiler is complaining that the label for your format statement isn't in the right place.

You could also get rid of the format statement completely.
Fortran:
read *, str
 
  • Like
Likes   Reactions: sysprog
Are you sure that you can put an entire array name in the print statement of line 38? It might just want a single ellement. In older FORTRANs one had to loop some way through the elements. The loop can be included in the print statement like:
WRITE(*,*) (x(i), y(i), i=1, n)
 
  • Like
Likes   Reactions: sysprog

Similar threads

  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K