Qbasicnews.com
February 26, 2020, 01:45:30 AM
 Welcome, Guest. Please login or register. 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length
 Home Help Search Login Register
 Pages: [1] 2 3
 Author Topic: Input 2 digit numbers  (Read 9206 times)
ghen55555
New Member

Posts: 8

 « on: November 11, 2003, 07:55:33 PM »

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
?
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #1 on: November 11, 2003, 08:05:14 PM »

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
 Logged

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.
ghen55555
New Member

Posts: 8

 « Reply #2 on: November 11, 2003, 09:34:43 PM »

BTW, how do i combine 2 variables, for example:
x=1
y=5

How do i make is print 15  instead of 1 5?
 Logged
na_th_an
*/-\*

Posts: 8244

 « Reply #3 on: November 11, 2003, 09:35:43 PM »

number = 10 *x +y
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Anonymous
Guest
 « Reply #4 on: November 11, 2003, 10:33:05 PM »

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 I hope this opened your mind a little.
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #5 on: November 11, 2003, 10:39:31 PM »

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:
 Logged

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.
Anonymous
Guest
 « Reply #6 on: November 12, 2003, 12:26:39 AM »

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:
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #7 on: November 12, 2003, 12:29:05 AM »

True...

You can validate that it is a number by checking the ASC() value of each digit...
 Logged

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.
Anonymous
Guest
 « Reply #8 on: November 12, 2003, 12:36:13 AM »

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
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #9 on: November 12, 2003, 12:53:26 AM »

Why do you have to make a goto loop?

Nothing needs a goto loop
 Logged

Plasma
Na_th_an

Posts: 1770

 « Reply #10 on: November 12, 2003, 12:56:48 AM »

Proper spaghetti code always needs at least one solid goto loop.
 Logged
Diroga
Been there, done that

Posts: 1087

 « Reply #11 on: November 12, 2003, 12:58:47 AM »

goto's are very nice in text rpg's
 Logged
Anonymous
Guest
 « Reply #12 on: November 12, 2003, 01:36:51 AM »

Quote from: "oracle"
Why do you have to make a goto loop?

Nothing needs a goto loop

oracle, how would you make a program that input text, and then had to redo the input line, becuase of improper input? im not trying to be a smartass, i just dont know of any other way. enlighten me. ::

edit: i figured it out :p

Code:

do
input a\$
loop until a\$ = WHATiWANTitTO\$

my bad  :x

but even still thats more code than
Code:

input a

yeah im done
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #13 on: November 12, 2003, 01:58:46 AM »

It's the 80/20 syndrome: 80% of the nessecary function can be achieved with 20% of the code/work

Moneo made a routine to get one and exactly one key from the user that was a correct key, It's at http://qbnz.com/pages/tutorials/algorsnz.txt, along with some other routines. Perhaps it could be modified to get two keys?
 Logged

Anonymous
Guest
 « Reply #14 on: November 12, 2003, 02:34:18 AM »

You could just do...

Code:
DO
LOCATE 1,1: PRINT "Input a 2-digit number."
Number = VAL(INPUT\$(2))
LOOP UNTIL Number > 0

However, this code poses a problem...
The text is not printed on the screen while being typed.

The code gets exactly 2 digits, though, and won't continue until the number's bigger than 0.
 Logged
 Pages: [1] 2 3
Jump to: