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

In summary, Lab5A introduces Fortran string handling capabilities. The program asks for a string to be entered in two integers and prints the name of the string along with the index of the first letter of the string in the upper word. The program also capitalizes the first letter of the string. Lastly, the program prints the cap at the end of the string.
  • #1
suezxc6
11
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
  • #2
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 sysprog
  • #3
i did just as you requested and still got this
 

Attachments

  • s.png
    s.png
    8.6 KB · Views: 213
Last edited by a moderator:
  • #4
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 sysprog
  • #5
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 sysprog

1. What does the error "Invalid form of PROGRAM statement" mean?

The error "Invalid form of PROGRAM statement" means that the structure of the PROGRAM statement in the Fortran code is not in the correct format. This could be due to missing or incorrect keywords, or incorrect placement of certain elements within the statement.

2. How can I fix the "Invalid form of PROGRAM statement" error?

To fix this error, you will need to carefully review the PROGRAM statement in your Fortran code and ensure that it follows the correct syntax and structure. Make sure that all necessary keywords are included and in the correct order, and that any necessary elements (such as the program name) are placed in the correct location within the statement.

3. Can this error be caused by a typo?

Yes, the "Invalid form of PROGRAM statement" error can be caused by a typo in the PROGRAM statement. Even a small mistake, such as misspelling a keyword, can result in this error. Therefore, it is important to double check the syntax and spelling in your PROGRAM statement to avoid this error.

4. Are there any common mistakes that can lead to this error?

Yes, there are a few common mistakes that can result in the "Invalid form of PROGRAM statement" error. These include missing necessary keywords, using incorrect keywords, and placing elements in the wrong location within the statement. It is also important to make sure that the program name is spelled correctly and matches the name used in other parts of the code.

5. Is there any specific software or tool that can help me identify and fix this error?

There are various Fortran compilers and debugging tools available that can help identify and fix the "Invalid form of PROGRAM statement" error. These include popular compilers like GNU Fortran and Intel Fortran, as well as debugging tools like GDB and TotalView. It is recommended to use these tools to assist in identifying and resolving any syntax errors in your Fortran code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
618
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
Back
Top