FORTRAN: Problem with converting reals to integers

  • Context: Comp Sci 
  • Thread starter Thread starter mattmac.nuke
  • Start date Start date
  • Tags Tags
    Fortran Integers
Click For Summary
SUMMARY

The discussion focuses on a FORTRAN program aimed at converting real numbers to integers for use in a do loop. The user encounters issues with scalar integer conversion, specifically when attempting to assign a real number to an integer variable. The solution proposed includes using the INT function to convert real numbers to integers, but emphasizes the need to store the result in a separate integer variable to avoid type conflicts. The program also includes error handling for user inputs related to loan parameters.

PREREQUISITES
  • Understanding of FORTRAN programming language
  • Knowledge of data types in FORTRAN (REAL and INTEGER)
  • Familiarity with control structures (DO loops and SELECT CASE statements)
  • Basic concepts of financial calculations (interest rates and loan payments)
NEXT STEPS
  • Learn about FORTRAN data type conversions and best practices
  • Research FORTRAN error handling techniques for user input validation
  • Explore formatting output in FORTRAN for better presentation of data
  • Study the use of CEILING and INT functions in FORTRAN for numerical operations
USEFUL FOR

Students learning FORTRAN, software developers working with financial applications, and anyone troubleshooting data type issues in programming.

mattmac.nuke
Messages
22
Reaction score
0

Homework Statement


I'm trying to convert data that's entered as a real number into integer data to be used in a do loop. The problem is that it keeps telling me that the numbers I've just converted are not scalar integers...


The Attempt at a Solution



program interest
IMPLICIT NONE
REAL :: r !Monthly interest rate
REAL :: pay !Montyly Payment Amount
REAL :: p !Initial loan amount (Money borrowed)
INTEGER :: n, i, j !Total number of payments to be made
REAL :: i_max !Maximum Interest rate
REAL :: i_min !Minimum Interest rate
REAL :: t_max !Maximum amount of Time for Loan payback
CHARACTER :: x



WRITE (*,*) 'COP 2271- Project 2'
WRITE (*,*) ' Interest rates '
WRITE (*,*) '-------------------------'
WRITE (*,*) 'Main Menu'
WRITE (*,*) ' A) Enter new Data'
WRITE (*,*) ' Q) Quit'
READ (*,*) x

DO
SELECT CASE (x)
CASE ('A','a')
DO
WRITE (*,*) 'Enter principle loan amount($)'
READ (*,*) p
WRITE (*,*) 'Enter maximum length of the loan(Years):'
READ (*,*) t_max
WRITE (*,*) 'Enter minimum interest rate(%):'
READ (*,*) i_min
WRITE (*,*) 'Enter maximum interest rate(%):'
READ (*,*) i_max
END DO
CASE ('Q','q')
EXIT
CASE DEFAULT
WRITE (*,*) 'Invalid Selection'
END SELECT

IF (p < 0) WRITE (*,*) 'ERROR- Principle amount must be positive!'
IF (t_max < 3) WRITE (*,*) 'ERROR- Maximum loan length must be greater than or equal to 3 (years)!'
IF (i_min < 0 .OR. i_min > 100 .OR. i_min > i_max) WRITE (*,*) 'Error- Interest rate must be between 0 and 100!'
IF (i_max < 0 .OR. i_max > 100) WRITE (*,*) 'Error- Interest rate must be between 0 and 100!'

END DO

i_max = CEILING(i_max)
i_min = INT(i_min)
t_max = CEILING(t_max)

DO i = 2, t_max
n = 12*i
pay = (r*p)/(1-((1 + r)**(-n)))

DO j = i_min, i_max

WRITE (*,'(3X,I3,8X,I3,5X,A,F10.2, 5X,A, F10.2)')


END DO
END DO


end program interest


-This is the program as I've written it so far, it's not finished... I'm also having problems with the formatting they want us to use for the tables.
 
Physics news on Phys.org
I don't think you can declare i_min as a REAL and then expect the type to convert to INTEGER after executing the statement: i_min = INT (i_min). You should assign the converted value of i_min to another variable of TYPE INTEGER.
 

Similar threads

Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K