Author Topic: Magic with printf and scanf.  (Read 122 times)

ko2

  • Regular Member
  • *
  • Posts: 2
Magic with printf and scanf.
« on: October 28, 2017, 04:03:16 AM »
Hello everybody.

I start learning masm, but I done't actully know what problem is.
The first example is not correct. The second work truly. Explain me please  :(
Code: [Select]
include /masm32/include/masm32rt.inc

.data

ConsoleTitle db 'Input', 0
mess_1 db 'Write arg1: ', 0
mess_2 db 'Write arg2: ', 0
mess_3 db ' + | - | * | / : ', 0

arg1 DWORD 0
arg2 DWORD 0
arg3 DWORD ?
res DWORD 0

formats db '%d', 0
formats2 db "%d %c %d = %d", 10, 0
formats3 db "%c", 0

.code

main PROC

invoke SetConsoleTitle, offset ConsoleTitle

invoke crt_printf, addr mess_1
invoke crt_scanf, addr formats, addr arg1

invoke crt_printf, addr mess_2
invoke crt_scanf, addr formats, addr arg2

invoke crt_printf, addr mess_3
invoke crt_scanf, addr formats3, addr arg3

invoke crt_printf, addr formats2, arg1, arg3, arg2, res
inkey

invoke ExitProcess, 0
main ENDP
END main
"Image1"



Code: [Select]
include /masm32/include/masm32rt.inc

.data

ConsoleTitle db 'Input', 0
mess_1 db 'Write arg1: ', 0
mess_2 db 'Write arg2: ', 0
mess_3 db ' + | - | * | / : ', 0

arg1 DWORD 0
arg2 DWORD 0
arg3 DWORD ?
res DWORD 0

formats db '%d', 0
formats2 db "%d %c %d = %d", 10, 0
formats3 db "%c", 0

.code

main PROC

invoke SetConsoleTitle, offset ConsoleTitle

invoke crt_printf, addr mess_3
invoke crt_scanf, addr formats3, addr arg3

invoke crt_printf, addr mess_1
invoke crt_scanf, addr formats, addr arg1

invoke crt_printf, addr mess_2
invoke crt_scanf, addr formats, addr arg2

invoke crt_printf, addr formats2, arg1, arg3, arg2, res
inkey

invoke ExitProcess, 0
main ENDP
END main
"Image2"

ko2

  • Regular Member
  • *
  • Posts: 2
Re: Magic with printf and scanf.
« Reply #1 on: October 28, 2017, 04:13:06 AM »
When I change formats3 db "%c", 0 to formats3 db "%s", 0 works perfectly.
Or  formats3 db " %c"
https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer#comment71802603_5240789
What does it mean?