Reading User Input: Storing Each Character

In summary, "Reading User Input: Storing Each Character" is a process used by computer programs to read and store individual characters entered by a user. It is important to store each character in order to process and manipulate the input, as well as perform error checking and validation. Common methods for reading and storing user input include functions like getchar() in C and input() in Python, as well as classes like Scanner in Java. Special characters can be handled through escape sequences or special functions, depending on the programming language. Best practices for reading and storing user input include validating the input, using loops, and choosing appropriate data structures for storage.
  • #1
ACLerok
194
0
I'm trying to write a program that will have the user enter in an expression in the form: A (operation) B (operation) C and print out the result. After the user enters in the expression, I want to parse out each individual digit or expression by reading the ASCII code of each individual character by storing each digit or operation in its own register. I'm having trouble reading each separate character after the user enters the expression.

li $v0, 8
la $a0, expr
li $a1, 20
syscall

How do I store each character? Something like the code below? (I know the code below will not work.) Am i supposed to store the string in a new register before reading each separate byte?

sb 0(expr), $t0
Also, since I'm only working with numbers between 20 and 99, and the operations + and *, is it ok to reserve only 5 bytes for the entire expression since 1 byte can represent one ASCII character?
 
Last edited:
Technology news on Phys.org
  • #2
The OP hasn't been around for 11+ years, but others might be interested.
ACLerok said:
I'm trying to write a program that will have the user enter in an expression in the form: A (operation) B (operation) C and print out the result. After the user enters in the expression, I want to parse out each individual digit or expression by reading the ASCII code of each individual character by storing each digit or operation in its own register. I'm having trouble reading each separate character after the user enters the expression.

li $v0, 8
la $a0, expr
li $a1, 20
syscall
You're apparently trying to input the expr string from the keyboard, using the read string system service, so you need memory somewhere that's capable of holding this string.

Here's a minimal way you could do this:
Code:
    .data
expr:  .asciiz  space 12

    .text
    la $a0, expr
    li $v0, 8        # Read string service
    syscall
It would be better to issue a prompt telling the user what needs to be entered, with maybe an example.
ACLerok said:
How do I store each character? Something like the code below? (I know the code below will not work.) Am i supposed to store the string in a new register before reading each separate byte?

sb 0(expr), $t0
In my example above, the entire string is store in the user data segment, beginning at the address of of the expr label.

If $t1 held the index of the current character, you could load it into $t2 with this code:
Code:
lb $t2, expr($t1)
ACLerok said:
Also, since I'm only working with numbers between 20 and 99, and the operations + and *, is it ok to reserve only 5 bytes for the entire expression since 1 byte can represent one ASCII character?
No, that's not enough. At minimum, an expression such as DD+DD+DD will take 9 bytes, two for each number, and two for the operators, plus one more for the terminating null character, and doesn't account for extra spaces entered by the user, as in 12 + 34 - 56.

That's really the easy part, assuming program reads the string in a single operation. You would then have to parse each character of the string, determining whether the first and possibly the second character are ASCII codes for numerals, then determine what operation is intended, and repeat for the 2nd number, 2nd operation, and 3rd number.

It's probably a lot easier to
Read a number (which could be 1 or 2 digits)
Read an operator (such as +, -, etc.)
Read another number
Read another operator
Read the last number

At that point your code could apply the operations to the number and get the final result.
 

1. What is "Reading User Input: Storing Each Character"?

"Reading User Input: Storing Each Character" is a process in which a computer program reads and stores each individual character entered by a user through a keyboard or other input device.

2. Why is it important to store each character when reading user input?

Storing each character allows for the input to be processed and manipulated in various ways by the program. It also allows for error checking and validation of the input.

3. What are some common methods for reading user input and storing each character?

Some common methods include using the getchar() function in C, the input() function in Python, or the Scanner class in Java.

4. How do you handle special characters or symbols when storing user input?

Special characters or symbols can be handled by using escape sequences or special functions, depending on the programming language. For example, in C++, the cin.get() function can be used to read special characters as individual characters. In Java, the next() method of the Scanner class can be used to read special characters as strings.

5. What are some best practices for reading user input and storing each character?

Some best practices include validating the input to ensure it is in the correct format and within the expected range, using loops to iterate through each character, and using appropriate data structures to store the input, such as strings or arrays.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top