News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Char/Number comparison trouble

Started by digitalquake, February 20, 2005, 12:11:47 AM

Previous topic - Next topic

digitalquake

Hello
How do I understand if the entered char is a letter(anything other than numbers basically)?... I'm working on conditionals and if a letter entered I want to prompt "you can only enter numbers".

Thanks

xxxx

#1
you can compare the input with the ascii code of the letter.the hex code for letter 'A' is 41 and the code for letter 'Z' is 5A.
so by doing typing:

                  CMP  variable,'A'
                   JB    PASS
                   CMP  variable,'Z'
                   JA    PASS
                   
                   ;error routine goes here

where variable is the input to be compared and PASS is the label that handles whatever it is that the code is supposed to do.you should also code for lowecase letters.
i could be wrong though and i think there's a better way to do it.

digitalquake

thanks for the suggestion but i think it wont work because the program works like this, if user enters 1, I prompt something, if enters 2 I promt something, if the user enters a number bigger than 4 then I prompt something in general, so in this case, letters, everything that is bigger than 4 or goes to that prompt. As you said A=41 so it goes there to and If I do your way if user enters 41 as a number it will jump somewhere else even though it is a number... Anyway thanks again for your time. I hope I find some solution to this :).

xxxx

"everything that is bigger than 4 or goes to that prompt. As you said A=41 so it goes there to and If I do your way if user enters 41 as a number it will jump somewhere else even though it is a number... Anyway thanks again for your time. I hope I find some solution to this :)."

i'd like to take another shot at ur prob.
assumming the user enters 41 from the keyboard,ASCII code 34h,31h would be read.therefore :

CMP variable,'A'

would be read as:

CMP 34,41

from here it is obvious that 34 is less than 41 so the program would jump to PASS won't it?

digitalquake

I gotta try but there should be a much simpler solution, I mean what I'm trying to make is quite simple....

Prompt > enter a number
If entered 1 prompt "You entered 1"
If entered 2 prompt "You entered 2"
If entered 3 prompt "You entered 3"
If entered bigger than 4 prompt "You entered a number bigger than 4"

And I have to prompt some message if letter entered.

Program works perfect till this part. Since everything I read is a number basically (even a letter) anything bigger than for goes to "You entered a number bigger than 4" prompt.

So when I enter A,D,Z I gotta now somehow that it is a char, is there somekind of flag that I should be looking at?

Thanks.


MichaelW

If the numbers will always be single digits, you could just test for the allowable numbers, and anything else would be a bad input. In pseudo code:

if input == '1'
  ---
else
  if input == '2'
    ---
  else
    if input == '3'
      ---
    else
      if input == '4'
        ---
      else
        error
      endif
    endif
  endif
endif


eschew obfuscation

digitalquake

only numbers are allowed but it can be more than one digit
basically

if (input == number )
{
    if (input ==1)
    Print "1";
    if (input ==2)
    Print "2";
    ...
    ...
    if( input > 4)
    // Any number bigger than 4, (3434,22,545)
    Print "bigger than 4"
}
else
{
print "You need to enter numbers"
}