The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: stapo34 on October 04, 2010, 01:09:18 PM

Title: how can i make my prog. accepts only 'y' &'n' char
Post by: stapo34 on October 04, 2010, 01:09:18 PM
how can i make my prog. accepts only 'y' &'n' char?

i really dont know what to do.



any help much appreciated.
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: BogdanOntanu on October 04, 2010, 02:40:53 PM
Do not double post, be patient and explain better your problem in such a way as more people can understand what you are trying to do and in what context and maybe this way you will get more answers faster.

You give too little context ... is this a windows or a dos application? is this a GUI or a console application? 32 bits or 16 bits or 64 bits?

Basically:
1) read key from user
2) compare key with "y"
3) if compare is equal then execute code for this case
4) if not then compare key with "n"
5) if compare is equal then execute code for this case
6) else emit an error message because key is not "y" or "n"

The exact specifics depend a lot on you giving more info...

And BTW we do not solve homework, this is a rule here
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: raymond on October 05, 2010, 12:43:34 AM
REMINDER

If your program is to be used by other than yourself alone, don't repeat the same error as some programmers used to do. Also check if the input is one of the upper case letters in addition to the lower case letters. There's nothing more aggravating than pressing "Y" (or "N") repeatedly and nothing happens. :boohoo:
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: dedndave on October 08, 2010, 01:23:15 AM
easy way is to simply force upper or lower case
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: oex on October 08, 2010, 01:56:14 AM
Quote from: raymond on October 05, 2010, 12:43:34 AM
There's nothing more aggravating than pressing "Y" (or "N") repeatedly and nothing happens. :boohoo:

cmp al, 'Y'
je RTFS
cmp al, 'N'
je RTFS

RTFS:
print "RTFS.... Press 'y' or 'n'.... I dont know why I bother sometimes.... I might as well do it all myself and tell you when I'm done.... You dont listen to a word I say...."
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: dedndave on October 08, 2010, 04:48:34 AM
and al,0DFh ;force upper case
cmp al, 'Y'
je RTFS
cmp al, 'N'
je RTFS
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: donkey on October 08, 2010, 08:12:10 AM
If you are using a GUI application and the program stops for user input you can use a message box with the MB_YESNO style, it will only display a yes and no button and will return IDYES or IDNO as results (IDYES being the default).

Edgar
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: ToutEnMasm on October 08, 2010, 10:36:59 AM
You can also intercept the message loop and:
Quote
.elseif uMsg == WM_CHAR         ;keyboard
      .if wParam == "Y" || wParam == "y"
for a control , you have to use this
Quote
   INVOKE     SetWindowLong, Hredit, GWL_WNDPROC, EventRichEdit

Then create "EventRichEdit" the proc for events

Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: jj2007 on October 08, 2010, 10:54:43 AM
Fascinating topic... 154 views and 8 answers so far :wink

include \masm32\include\masm32rt.inc

.code
AppName db "Masm32:", 0

start:
@@: print "Press y[es] or n[o]", 13, 10
getkey
or al, 32  ; lowercase
cmp al, "y"
je PressedYes
cmp al, "n"
jne @B

PressedNo:
inkey "Oh no..."
exit
PressedYes:
inkey "Oh yes..."
exit

end start
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: ToutEnMasm on October 08, 2010, 01:43:12 PM

Quote
Fascinating topic... 154 views and 8 answers so far
Perhaps if the author can say he want to use this under:
                    Windows
                    Dos
                    In a windows or without window
Answers will be less numerous.
Title: Re: how can i make my prog. accepts only 'y' &'n' char
Post by: Vortex on October 09, 2010, 06:34:34 PM
Hi stapo34,

Here is a quick example for you :

.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib


.data

msgtable    dd OFFSET yes , OFFSET no
text1       db 'Hit yes or no',0
capt        db 'Hello',0
yes         db 'You pressed yes',0
no          db 'You pressed no',0



.code

start:

    invoke  MessageBox,0,ADDR text1,ADDR capt,MB_YESNO

    shl     eax,2       ; eax = eax * 4

                        ; IDYES = 6   ,   IDNO = 7

    add     eax,OFFSET msgtable - 4 * IDYES

    invoke  MessageBox,0,DWORD PTR [eax],ADDR capt,MB_OK
         
    invoke  ExitProcess,0

END start