How to convert letters to numbers?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

How to convert letters to numbers?

Post by SoKeT »

Hi, I'm doing an Arabic-Roman and vice versa numerals converter. When I thought I had everything complete, a problem appeared:

Code: Select all

INPUT "Roman Number: ", r$
r1$ = MID$(r$, 1, 1)
r2$ = MID$(r$, 2, 1)
r3$ = MID$(r$, 3, 1)
...
r15$ = MID$(r$, 15, 1)
'This takes each of the input characters into a different variable. I made 15 because that's the maximum amount of numbers in the Roman numerals (MMMDCCCLXXXVIII)
Now I have to convert each letter to it's corresponding value, and then Calculate the arabic number using a formula that I already have. The only thing I could think to accomplish this is the following:

Code: Select all

IF r1$ = "M" then r1$ = 1000
...
IF r1$ = "C" then r1$ = 100
...
IF r1$ = "X" then r1$ = 10
...
7 different letters by 15 maximum letters = 105 lines of code. It's not optimal, but I would have been happy if it worked. Sadly, it didn't. An error that says something like "Types don't match" appears on the bolded part: r1$ = 1000

Is there a way to solve this by changing the variable type or do I have to rethink the whole thing differently?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: How to convert letters to numbers?

Post by burger2227 »

You cannot assign a string variable a numerical value.

Code: Select all

IF r1$ = "M" then r1 = 1000
...
IF r1$ = "C" then r1 = 100
...
IF r1$ = "X" then r1 = 10
Roman numerals are difficult to translate to numerical values as the positioning can determine if the value is added or subtracted. XI = 11 while IX = 9.
So you will have to not only determine each letter value, but you will have to consider the position of that value in the letter string.

The first thing to do is scan the Roman Numeral string:

Code: Select all

INPUT "Enter a valid Roman Number: ", r$

L = LEN(r$)

REDIM Array(L)

FOR i = 1 TO L
  temp$ = MID$(r$, i, 1)
  SELECT CASE temp$
      CASE "I":  Array(i) = 1
      CASE "V": Array(i) = 5
      CASE "X": Array(i) = 10
      '... L , D, M , etc
     CASE ELSE: Array(i) = 0  'eliminate irrelevant letters
  END SELECT
NEXT i
I just put each letter value into an array, but you could use a FOR counter loop to do the calculations too.
You can check the Array to determine when greater values follow or precede lower values also.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

Re: How to convert letters to numbers?

Post by SoKeT »

I'm dumb, I just had to not put a string on the variable that saves the number. I'm glad I asked though, because it's much easier to gather the input with the FOR NEXT loop, which I completely forgot about since it's been a while since I made a lot of QBASIC programs (not like I knew a lot anyway). You might remember me from this topic, I certainly remember you. That was the last time I programmed something. By the way, I didn't see your last message there, but I didn't have a problem with SLEEP. Basically I added a DO LOOP that kept the program running unaltered until you press Esc, which ends the program. I got a bit offtopic here :P

On to the topic, the actual converter was easy to do once I could atribute the input letters their Arabic values. I just had to sum up all the values and then check if there was a number at the right greater than one adjacent at the left, for all the numbers. If that's the case, it just deducts the smaller number twice from the total amount (one to fix the previous sum and one to do the actual deduction).

Code: Select all

IF r1 < r2 THEN r0 = r0 - (2 * r1)
All in all it was a nice code to work on, I made all of it today because I missed the challenge of creating a functional program.

Thanks for your help.
Post Reply