Fortran: Word input rather than number?

  • Context: Fortran 
  • Thread starter Thread starter gmcke1
  • Start date Start date
  • Tags Tags
    Fortran Input
Click For Summary
SUMMARY

In Fortran, specifically using the GNU g95 compiler, users can input strings rather than numbers by utilizing the READ statement. The example provided demonstrates how to read a character string into a variable and compare it using the equality operator (==) in Fortran 95. For earlier versions, the comparison operator should be .EQ. instead of ==. This method allows for user-defined string inputs, such as 'exit', to control program flow.

PREREQUISITES
  • Understanding of Fortran programming language syntax
  • Familiarity with the GNU g95 compiler
  • Knowledge of character data types in Fortran
  • Basic concepts of conditional statements in programming
NEXT STEPS
  • Explore string manipulation functions in Fortran
  • Learn about command line parameters in Fortran 2003 and later
  • Investigate the differences between Fortran 95 and earlier versions
  • Study error handling in Fortran for user input
USEFUL FOR

This discussion is beneficial for Fortran programmers, particularly those working with user input and string handling, as well as educators teaching Fortran syntax and functionality.

gmcke1
Messages
9
Reaction score
0
I would like to input a word in the fortran command line rather than a number. For example, if I wanted to exit a program with the word 'exit' how would that be done?

Thanks for the help :D

-Glenn
 
Technology news on Phys.org
in the fortran command line
Do you really mean "command line" here, as in "command line parameter". Or are you actually inquiring about reading (and comparing) strings.

I don't think there is a way to access command line parameters in standard fortran (at least up to F95) so I assume you mean the latter.

Code:
character(20) name

print "(a$)", 'Enter your name> '
read *, name
if (name == 'uart') print *,'Cool name dude.'

The above works for me in Fortran 95 (the gnu g95 compiler). In earlier Fortran versions you might have to use .EQ. instead of ==
 
That is exactly what I was looking for. I didn't know how to use words as input for Fortran until now :DDDDDDDDDDDDDD Thank you so much.

-Glenn
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K