Skip to main content
Engineering LibreTexts

6.2: Input – Read

  • Page ID
    54264
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    To obtain information from the user, a read statement is used. For example, to declare the variables num1, num2,

    integer :: ans1, ans2
    

    then read a value for ans1 from the user,

    read (*,*) ans1
    

    Which will read a number from the user entered on the keyboard into the variable ans1. The (*,*) means to send it to read the information in 'free format'. The free format allows the Fortran compiler to determine the appropriate format for the information being read.

    Multiple variables can be read with one read statement. For example, using the previous declarations,

     read (*,*) ans1, ans2
    

    will read two values from the user into the variables ans1 and ans2.

    Since the read is using free format, two numbers will be required. The numbers can be entered on the same line with one or more spaces between them or on separate lines. The read will wait until two numbers are entered.

    When reading information from the user, it is usually necessary to provide a prompt in order to ensure that the user understands that input is being requested by the program. A suitable write statement with an appropriate string, followed by a read statement will ensure that the user is notified that input is being requested.

    For example, to read a date, a program might request month, day, and year as three separate variables. Given the following declarations,

    integer :: month, day, year
    

    the program might prompt for and read the data in the following manner,

    write (*,*) "Enter date (month, day, and year)"
    read (*,*) month, day, year
    

    Since the program is requesting three integers, three integers must be entered before the program continues. The three numbers may be entered on one line with a single space between them, with multiple spaces or tab between them, or even on three different lines as in the following examples:

    Enter date (month, day, and year)
    10 17 2009
    
    Enter date (month, day, and year)
    10
    17
    2009
    
    Enter date (month, day, and year)
    10    17    2009 
    

    The type of number requested here is an integer, so integers should be entered. Providing a real number or character (e.g., letter) would generate an error. Later chapters will address how to deal with such errors.


    This page titled 6.2: Input – Read is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?