Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input 2 digit numbers
#1
Lets say the i have the code:

INPUT "Enter a 2-digit number";number

How could i take the variable "number" and break it apart into 2 serperate variables?
For example id the user enters number = 34
How could i make number1 = 3
and number2 = 4
?
Reply
#2
The answer is:

Exactly how you would do it with mathematics.

There are several such approaches.

The easiest, conceptually, is to say (given x=input, A=tens digit, and B=ones digit):

b = x MOD 10
a = x - b
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#3
BTW, how do i combine 2 variables, for example:
x=1
y=5

How do i make is print 15 instead of 1 5?
Reply
#4
number = 10 *x +y
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
Hey i dont know if this is considered 'chintz' or bad proggin 'practice', but you could also take the nums and convert them to strings, and manipulate them, fo examp:

Code:
INPUT "Please enter a two-digit number"; Num    'Let's say 47

StNum$ = LTRIM$(RTRIM$(STR$(Num)))
                                                   'If you don't know about R and Ltrim,
                                                                             'e-mail me.
Tens = VAL(LEFT$(StNum$, 1))
Ones = VAL(RIGHT$(StNum$, 1))            
                                                 'If you don't know about Right$, Left$,
                                                                 'or VAL e-mail me. lol.
PRINT "Tens digit is:"; Tens
PRINT "Ones digit is:"; Ones

It's the same if you want to assemble digits.

Code:
X = 5
Y = 2

Number = VAL(LTRIM$(RTRIM$(STR$(X))) + LTRIM$(RTRIM$(STR$(Y))))
                        
PRINT Number

I know this seems bulky, but there's actually times where this can come in handy. I can't go into it right now cause I'm getting hounded to get off the computer Tongue I hope this opened your mind a little.
Reply
#6
Hm... well... in that case . . .

Why not just say:

Code:
INPUT "gimme a number", n$

x% = VAL(MID$(n$, 1, 1 ))
y% = VAL(MID$(n$, 2, 1))

:lol:
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#7
yeah that IS a lot simpler...

but i mean he said
Quote:Lets say i have the code:

INPUT "Enter a 2-digit number";number
so i was just going off what he said.

plus if you use a string u leave the possibilty that someone could screw up and enter a word, and then all hell breaks loose.

sometimes simple isnt better :wink:
Reply
#8
True...

You can validate that it is a number by checking the ASC() value of each digit...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#9
even so then youd have to use a sloppy goto loopif they aren't, when you could just let qbasic give a 'redo from start' error for you. i dunno let's make peace brotha lol 8)
Reply
#10
Why do you have to make a goto loop?

Nothing needs a goto loop Smile
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)