How Can a Single READ Statement Handle Multiple Data Types in Programming?

  • Fortran
  • Thread starter cibui
  • Start date
  • Tags
    Programming
In summary: The argument will then have to be further parsed so that it can be evaluated as an expression.In summary, the user is asking how to make a READ statement that can read both numbers and letters/words, or functions, at the same time. They want to know if this can be done with only one statement or if they need to break it into several statements. The expert responds by explaining that a READ statement can only be set up to read numbers or strings of characters, not functions. The logic for recognizing and parsing functions must be implemented in the program. The expert also clarifies that everything will come into the program as a string of characters, and the program will need to have logic to separate out the function name and evaluate the argument as
  • #1
cibui
9
0
Hi guys,

straight to the point, how do you make your READ statement to be able to read both numbers and letters(words) or functions at the same time? okay, for example, i want the user to input a continuous function. what if the function is, let say, sin(x)? do i need to break the READ statement into several READ statements or it can be done with only one statement? i was only taught to use "READ *, " but never told how does this really work.

I'm no programmer and have no programming background, so please help me :redface:
greatly appreciated
 
Last edited:
Technology news on Phys.org
  • #2
This is a harder problem than you might realize, since your program has to parse the input text to recognize function names.

A READ statement can be set up to read numbers (either real or integer) or strings of characters. It can't be set up to read functions such as sqrt or sin. There must be logic in the program to match input strings to a list of functions. If you have no programming experience, this will probably be beyond your abilities.
 
  • #3
Thanks for the reply Mark,
okay, so it is not possible afterall...
guess i will have to break the input structure into several steps then

anyway tyvm
 
  • #4
I didn't say it wasn't possible. I said that with your lack of experience it would be very difficult.

In your first post you talked about "break(ing) the READ statement into several READ statements..." That's not what you need to do.

Your program needs to read the input as a string, and then it needs to parse the string to determine if the input string contains the name of a function, such as sin, or tan, or ln, or whatever.
 
  • #5
i know that read statement can read strings of texts but the thing is what if it involves operable numbers? e.g. sin2x. i want it so that the inputted "2" by the user can be processed by the program as an operable number not as a part of a string of text. can READ actually do that?
thanks for your help
 
  • #6
Everything will necessarily have to come into the program as a string of characters. In your example, if the user enters sin2x, your program will read this as a character string. Your program will then have to have logic so that it can separate out the function name (sin) from the argument (2x).
 

Related to How Can a Single READ Statement Handle Multiple Data Types in Programming?

1. What are READ statements in programming?

READ statements in programming are commands that are used to take user input and store it in a variable. This allows the program to interact with the user and modify its behavior based on the input provided.

2. How do READ statements work?

When a READ statement is encountered in a program, the program will pause and wait for the user to input data. Once the data is entered, it is stored in the specified variable and the program will continue to execute.

3. What is the syntax for a READ statement?

The syntax for a READ statement varies depending on the programming language, but it typically consists of the keyword "READ" followed by the name of the variable where the input will be stored. Some languages may also include additional parameters for formatting or error handling.

4. What happens if the user inputs incorrect data?

If the user inputs incorrect data, the program may throw an error or the variable may be assigned a default value. Some programming languages have built-in functions to handle user input validation, which can help prevent errors from occurring.

5. How are READ statements different from other input methods?

READ statements are specifically used for taking user input in programming. Other input methods, such as command line arguments or file input, may also be used to provide data to a program, but they serve different purposes and have different syntax.

Similar threads

  • Programming and Computer Science
Replies
22
Views
963
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top