Fortran: Word input rather than number?

In summary, the conversation discusses the use of words as input in Fortran, specifically in the command line. The individual was unsure if it was possible to input a word instead of a number and asked for help on how to do so. The other person explains that it is possible and provides a code example using the character datatype and the print and read commands. The conversation ends with gratitude for the help.
  • #1
gmcke1
9
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
  • #2
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 ==
 
  • #3
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
 

1. What is Fortran?

Fortran is a high-level programming language used primarily for scientific and engineering applications. It stands for "Formula Translation" and was originally developed by IBM in the 1950s.

2. How do I input words instead of numbers in Fortran?

In order to input words, also known as strings, in Fortran, you need to use the CHARACTER data type. This data type allows you to store and manipulate strings of characters, such as words or sentences, in your program.

3. Can I mix words and numbers in Fortran?

Yes, you can mix words and numbers in Fortran. As mentioned before, you can use the CHARACTER data type for words and the INTEGER or REAL data types for numbers. You can also convert between these data types using built-in functions.

4. How do I read input words from the user in Fortran?

To read input words from the user, you can use the READ statement and specify the CHARACTER data type. This will prompt the user to enter a string of characters and store it in a variable that you can then use in your program.

5. Can I use words as variables in Fortran?

Yes, you can use words as variables in Fortran. However, there are some restrictions on what characters can be used in variable names. They must start with a letter and can only contain letters, numbers, and underscores. Additionally, some words are reserved keywords in Fortran and cannot be used as variable names.

Similar threads

  • Programming and Computer Science
Replies
20
Views
3K
Replies
11
Views
2K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
20
Views
5K
Back
Top