0

I am working on a simple arm assembly program that gets input from the user, and calculates the area of a shape.

The program is supposed to provide an answer, wait for the user to press enter, and then display the main menu again:

The area of the rectangle is: 4
press enter to continue ...

When I run my program, it immediatly skips over my wait_for_enter subroutine, grabs whatever is still in the input buffer, and then sees that as invalid input:

The area of the rectangle is: 4
Press enter to continue...
Welcome to Area Calculation Program
Enter the shape number:
1. Triangle
2. Rectangle
3. Trapezoid
4. Square

Or enter q to quit
Invalid input. Please try again
Welcome to Area Calculation Program
Enter the shape number:
1. Triangle
2. Rectangle
3. Trapezoid
4. Square

Or enter q to quit

Currently, I am attempting to clear the buffer after I output the result:

@------------------------------------------------------------
wait_for_enter:
   @ Wait for the user to hit the enter key so that the user can see the result
   @ save the link register so we can return to the caller
   push {lr}

   @ clear the input buffer
   ldr r0, =strInputPattern
   ldr r1, =strInputError   @ Put address into r1 for read.
   bl scanf                 @ scan the keyboard.

   @ print the prompt
   ldr r0, =enterMsg
   bl printf

   @ read the input
   ldr r0, =strInputPattern
   ldr r1, =strInputError   @ Put address into r1 for read.
   bl scanf                 @ scan the keyboard.

   @ return to the caller
   pop {pc}

@------------------------------------------------------------


.data

.balign 4
strInputPattern: .asciz "%[^\n]" @ Used to clear the input buffer for invalid input. 

.balign 4
strInputError: .skip 100*4  @ User to clear the input buffer for invalid input. 

.balign 4
enterMsg: .asciz "Press enter to continue\n"

but it seems like that is not clearing everything out. if I use %c instead of %[^\n], It seems to gobble up a lot of my input. What is the correct way to do this?

John Ingram
  • 164
  • 1
  • 12
  • 1
    See [this](https://stackoverflow.com/q/40038538/17208187) question. In short `%[^\n]` will not "clear" the input buffer, it will read everything until a new line, and trigger an undefined behaviour if more than `sizeof(strInputError)` is retreived. – Flexz Feb 10 '23 at 05:42

0 Answers0