Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiler Error help

Status
Not open for further replies.

WSUPhys

Technical User
Feb 6, 2013
20
US
I have written my program, it compiles properly but then when I go to run the .exe file it does the following: ./finalprojectavg.exe
Reading file...
File cannot be opened !
Floating point exception

Can somebody explain to me what this is? I have never had a program pop-up a floating point exception
Code:
program WSUPhys_csv
  implicit none

  integer :: stat, num_lines, ivar1, ivar2, ivar3
  integer :: Sum_ivar1, Sum_ivar2, Sum_ivar3, Avg_ivar1, Avg_ivar2, Avg_ivar3,MaxVert,MaxSide,MaxForward
  real :: rvar
  character*80 :: line
  
  write(*,*) "Reading file..."

  ! open input file
  open(10,file='DATA-002.csv',status='old',iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99   
  end if

  num_lines = 0
  Sum_ivar1 = 0
  Sum_ivar1 = 0
  Sum_ivar1 = 0
  MaxVert=0
  MaxSide=0
  MaxForward=0
  do
    read(10,'(A)',end=99, iostat=stat) line 
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    ! skip comment/header lines beginning with ";"
    if (adjustl(trim(line(1:1))) .eq. ';') then
      cycle
    end if
    ! read string line into numeric variables
    read(line,*) rvar, ivar1, ivar2, ivar3
    num_lines = num_lines + 1    
    if (num_lines <=  10) then
      write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
    end if
  Sum_ivar1 = Sum_ivar1 + ABS(ivar1) 
  Sum_ivar2 = Sum_ivar2 + ABS(ivar2) 
  Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
  If (ABS(ivar1) > MaxVert) then
     MaxVert=ivar1
    end if
    If (ABS(ivar2) > MaxSide) then
     MaxSide=ivar1
    end if
    If (ABS(ivar3) > MaxForward) then
     MaxForward=ivar1
    end if
 
   
  end do
  ! close file
  99 continue
  Avg_ivar1 = (Sum_ivar1 / num_lines)
  Avg_ivar2 = (Sum_ivar2 / num_lines)
  Avg_ivar3 = (Sum_ivar3 / num_lines)
  close (10)
  !
  write(*,*) '...done.'
  write(*,*) 'Lines processed: ', num_lines
  write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
  write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
  write(*,*) 'Average forwards acceleration', Avg_ivar3/1000
  write(*,*) 'Maximum vertical acceleration', MaxVert
  write(*,*) 'Maximum sideways acceleration', MaxSide
  write(*,*) 'Maximum forward acceleration',  MaxForward
 
end program WSUPhys_csv

DATA-002.csv file is here:Link
 
The error message seems to come from here
Code:
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99   
  end if
IMO the cause could be, that the program cannot find the file. Place the file in the same path where you created the executable.
 
Also label 99 should be before the close statement. You are dividing by zero, hence the floating point exception.
 
Yes, compute and print the results only when everything with file was OK, i.e. when stat = 0.
 
does this look right? I removed the close file before the continue.

Code:
program WSUPhys_csv
  implicit none

  integer :: stat, num_lines, ivar1, ivar2, ivar3
  real :: rvar
  character*80 :: line
  
  write(*,*) "Reading file..."

  ! open input file
  open(10,file='DATA-002.csv',status='old',iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99   
  end if

  num_lines = 0
  Sum_ivar1 = 0
  Sum_ivar1 = 0
  Sum_ivar1 = 0
  MaxVert=0
  MaxSide=0
  MaxForward=0
  do
    read(10,'(A)',end=99, iostat=stat) line 
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    ! skip comment/header lines beginning with ";"
    if (adjustl(trim(line(1:1))) .eq. ';') then
      cycle
    end if
    ! read string line into numeric variables
    read(line,*) rvar, ivar1, ivar2, ivar3
    num_lines = num_lines + 1    
    if (num_lines <=  10) then
      write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
    end if
    If (ABS(ivar1) > MaxVert) then
     MaxVert=ivar1
    end if
    If (ABS(ivar2) > MaxSide) then
     MaxSide=ivar1
    end if
    If (ABS(ivar3) > MaxForward) then
     MaxForward=ivar1
    end if
  Sum_ivar1 = Sum_ivar1 + ABS(ivar1) 
  Sum_ivar2 = Sum_ivar2 + ABS(ivar2) 
  Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
   
  end do

  99 continue
  Avg_ivar1 = (Sum_ivar1 / num_lines)
  Avg_ivar2 = (Sum_ivar2 / num_lines)
  Avg_ivar3 = (Sum_ivar3 / num_lines)
  close (10)
  !
  write(*,*) '...done.'
  write(*,*) 'Lines processed: ', num_lines
  write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
  write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
  write(*,*) 'Average forwards acceleration', Avg_ivar3/1000
  write(*,*) 'Maximum vertical acceleration', MaxVert
  write(*,*) 'Maximum sideways acceleration', MaxSide
  write(*,*) 'Maximum forward acceleration',  MaxForward
 
end program WSUPhys_csv
 
If it works when you run it, then it probably is correct. Try running it with and without the file being present. It may give a rubbish result but it shouldn't crash.
 
I thought something like this
Code:
[COLOR=#a020f0]program[/color] WSUPhys_csv
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#2e8b57][b]integer[/b][/color] :: stat, num_lines, ivar1, ivar2, ivar3, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
             MaxVert,MaxSide,MaxForward
[COLOR=#2e8b57][b]  real[/b][/color] :: rvar, Sum_ivar1, Sum_ivar2, Sum_ivar3, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight] 
          Avg_ivar1, Avg_ivar2, Avg_ivar3
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]80[/color] :: line
[COLOR=#2e8b57][b]  logical[/b][/color] :: err
  
  err [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff].false.[/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading file..."[/color]

  [COLOR=#0000ff]! open input file[/color]
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]10[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'DATA-002.csv'[/color],[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    err [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff].true.[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'File cannot be opened !'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]   
  [COLOR=#804040][b]end if[/b][/color]

  num_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  Sum_ivar1 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  Sum_ivar2 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  Sum_ivar3 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  MaxVert[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
  MaxSide[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
  MaxForward[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]10[/color],[COLOR=#ff00ff]'(A)'[/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]99[/b][/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat) line 
    [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
      err [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff].true.[/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error reading data !'[/color]
      [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]99[/color]
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#0000ff]! skip comment/header lines beginning with ";"[/color]
    [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]adjustl[/color]([COLOR=#008080]trim[/color](line([COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]1[/color]))) [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]';'[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]cycle[/b][/color]
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#0000ff]! read string line into numeric variables[/color]
    [COLOR=#804040][b]read[/b][/color](line,[COLOR=#804040][b]*[/b][/color]) rvar, ivar1, ivar2, ivar3
    num_lines [COLOR=#804040][b]=[/b][/color] num_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]    
    [COLOR=#804040][b]if[/b][/color] (num_lines [COLOR=#804040][b]<=[/b][/color]  [COLOR=#ff00ff]10[/color]) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) num_lines, [COLOR=#ff00ff]':'[/color], rvar, ivar1, ivar2, ivar3
    [COLOR=#804040][b]end if[/b][/color]
    Sum_ivar1 [COLOR=#804040][b]=[/b][/color] Sum_ivar1 [COLOR=#804040][b]+[/b][/color] [COLOR=#008080]ABS[/color](ivar1) 
    Sum_ivar2 [COLOR=#804040][b]=[/b][/color] Sum_ivar2 [COLOR=#804040][b]+[/b][/color] [COLOR=#008080]ABS[/color](ivar2) 
    Sum_ivar3 [COLOR=#804040][b]=[/b][/color] Sum_ivar3 [COLOR=#804040][b]+[/b][/color] [COLOR=#008080]ABS[/color](ivar3)
    [COLOR=#804040][b]If[/b][/color] ([COLOR=#008080]ABS[/color](ivar1) [COLOR=#804040][b]>[/b][/color] MaxVert) [COLOR=#804040][b]then[/b][/color]
      MaxVert[COLOR=#804040][b]=[/b][/color]ivar1
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#804040][b]If[/b][/color] ([COLOR=#008080]ABS[/color](ivar2) [COLOR=#804040][b]>[/b][/color] MaxSide) [COLOR=#804040][b]then[/b][/color]
      MaxSide[COLOR=#804040][b]=[/b][/color]ivar1
    [COLOR=#804040][b]end if[/b][/color]
    [COLOR=#804040][b]If[/b][/color] ([COLOR=#008080]ABS[/color](ivar3) [COLOR=#804040][b]>[/b][/color] MaxForward) [COLOR=#804040][b]then[/b][/color]
      MaxForward[COLOR=#804040][b]=[/b][/color]ivar1
    [COLOR=#804040][b]end if[/b][/color]   
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#0000ff]! close file[/color]
  [COLOR=#6a5acd]99[/color] [COLOR=#804040][b]continue[/b][/color]
  [COLOR=#804040][b]close[/b][/color] ([COLOR=#ff00ff]10[/color])

  [COLOR=#0000ff]! when file processing OK[/color]
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#804040][b].not.[/b][/color] err) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'...done.'[/color]
    [COLOR=#0000ff]! compute averages[/color]
    Avg_ivar1 [COLOR=#804040][b]=[/b][/color] (Sum_ivar1 [COLOR=#804040][b]/[/b][/color] num_lines)
    Avg_ivar2 [COLOR=#804040][b]=[/b][/color] (Sum_ivar2 [COLOR=#804040][b]/[/b][/color] num_lines)
    Avg_ivar3 [COLOR=#804040][b]=[/b][/color] (Sum_ivar3 [COLOR=#804040][b]/[/b][/color] num_lines)
    [COLOR=#0000ff]![/color]

    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Lines processed: '[/color], num_lines
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Average vertical acceleration'[/color], Avg_ivar1
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Average sideways acceleration'[/color], Avg_ivar2
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Average forwards acceleration'[/color], Avg_ivar3
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Maximum vertical acceleration'[/color], MaxVert
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Maximum sideways acceleration'[/color], MaxSide
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Maximum forward acceleration '[/color],  MaxForward
  [COLOR=#804040][b]end if[/b][/color]  
[COLOR=#a020f0]end program[/color] WSUPhys_csv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top