Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: Licentia on July 30, 2009, 04:36:39 AM



Title: Print 000 As Numerical Variable
Post by: Licentia on July 30, 2009, 04:36:39 AM
 Hello!

 I know that I can print 001 as a "$tring Variable." But I want to print 001 as a "Numerical Variable." Any suggestions how I might do this?

 Licentia


Title: Re: Print 000 As Numerical Variable
Post by: Ninkazu on July 30, 2009, 08:47:05 AM
PRINT USING has formatting options for this I think.


Title: Re: Print 000 As Numerical Variable
Post by: Licentia on July 30, 2009, 04:42:14 PM
 I cannot believe how ridiculously difficult it is to print 001 in a text file!!!

 How about this then, is there a way to take 3 numerical variables like 0 and 0 and 1 and combine them into one variable like so: 001 ?

 I tried switching my numeric variable to a string variable before printing but it still loses the zero(s) off the front. I tried switching to a decimal point number and that cuts the zero(s) off the back.

 I want to input Pick3 combinations from a text file. I can't do line input because that is only for strings. So I have to take each number one at a time N1;N2;N3 then try to convert them to a single integer. I thought I had done this successfully as follows: N = N1*100+N2*10+N3*1 But that dang zero keeps messing me up!


Title: Re: Print 000 As Numerical Variable
Post by: Clippy on July 30, 2009, 04:57:09 PM
The KEYWORD you used is TEXT. Text = String not numbers.

Try this:
    number$ = LTRIM$(STR$(num%)) ' convert number for string display
    zeros$ = "00"
    zeros$ = MID$(zeros$, 1, 3 - LEN(number$)) 'zero place values are allowed by MID$
    result$ = zeros$ + number$

This will add zero's to the left up until 100.

You can then use PRINT # or WRITE # to place it in a file.

You can convert it back to a numerical value with VAL(result$), but num% already holds that value anyhow.

PRINT USING cannot format 0's ahead of numbers either.


Title: Re: Print 000 As Numerical Variable
Post by: Licentia on July 30, 2009, 05:16:32 PM
 I pasted the code above into my program but the output was the same. Perhaps I needed to modify the code to suit my program, I'm not sure as I haven't used code like that before.

 But your response did get me to thinking and I came up with the code inbetween the '******. My output is ugly with spaces between the string and the numeric variables but that's fine so long as it works.

 Output:

0 45
0 99
0 41
139
132
298
317
00 0
00 3

Code:
CLS

CLEAR



RANDOMIZE TIMER



OPEN "Pick3" FOR INPUT AS #1

OPEN "000-499" FOR OUTPUT AS #2

OPEN "500-999" FOR OUTPUT AS #3



DO WHILE NOT EOF(1)



 INPUT #1, N1

 INPUT #1, N2

 INPUT #1, N3



 NN1 = N1 * 100 

 NN2 = N2 * 10

 NN3 = N3 * 1

 N = NN1 + NN2 + NN3



'******************************
 IF NN1 = 0 AND NN2 <> 0 THEN

  N$ = "0"

 ELSEIF NN1 = 0 AND NN2 = 0 THEN

  N$ = "00"

 ELSE

  N$ = "1"

 END IF


'******************************

 IF N < 500 THEN

 

  IF N$ = "1" THEN

   PRINT #2, N

  ELSE

   PRINT #2, N$; N

  END IF


 
 END IF



 IF N >= 500 THEN



  IF N$ = "1" THEN

   PRINT #3, N

  ELSE

   PRINT #3, N$; N

  END IF




 END IF



LOOP


 Thanks everyone! Stupid Microsoft won't let me print a dang zero!


Title: Re: Print 000 As Numerical Variable
Post by: Clippy on July 30, 2009, 08:25:10 PM
When you want to print consecutive numbers, you need to change them to a string value and edit it.

number$ = STR$(num%) 'eliminates a number's end space

To get rid of the leading space for the number's sign ( a space when positive),
you have to use LTRIM$(number$) to get rid of that space.

This is true when using PRINT or PRINT # or WRITE #. That is why there is a space in your returns. Number values will ALWAYS add that space!

WRITE # will send a string value with quotation marks.
PRINT # will send the value without quotation marks to a text file.
INPUT # can read PRINT # text as numerical or string values!


Title: Re: Print 000 As Numerical Variable
Post by: Moneo on July 30, 2009, 09:05:10 PM
Print a positive number from 0 to 999, with leading zeros.

' x is a positive nuimber from 0 to 999.

print right$("00"+ltrim$(str$(x)),3)

Regards..... Moneo


Title: Re: Print 000 As Numerical Variable
Post by: Licentia on August 02, 2009, 04:31:09 PM
 LOL! I don't even need to print N. I can just print N1;N2;N3.

 What was I thinking? I just need to compare N as an integer, there is no necessity of printing N when N1;N2;N3 are available.

 Sorry to waste everyone's time and thanks for trying to help!


Title: Re: Print 000 As Numerical Variable
Post by: Clippy on August 02, 2009, 08:08:55 PM
GIRL YOU JUST DON'T GET IT!

NOW YOU ACT LIKE WE WERE CLUELESS?

STICK TO KNITTING!

I TOLD YOU THAT IN MY FIRST POST! Geeesh....learn how to read too while you're at it!


Title: Re: Print 000 As Numerical Variable
Post by: Moneo on August 02, 2009, 09:18:09 PM
LOL! I don't even need to print N. I can just print N1;N2;N3.

 What was I thinking? I just need to compare N as an integer, there is no necessity of printing N when N1;N2;N3 are available.

 Sorry to waste everyone's time and thanks for trying to help!

I tried workng with N1, N2, and N3, and it's actually more code because you need to convert each of these three variable to a string and do a ltrim$ on each to get rid of the leading space on each of the string variables.

Regards..... Moneo


Title: Re: Print 000 As Numerical Variable
Post by: Clippy on August 02, 2009, 10:15:38 PM
Come on Moneo, HOW ELSE are you going to print leading zeroes.

YOU HAVE to add a string to a trimmed string representation of a number.

This question has been ASKED AND ANSWERED too many times already!

We don't need a rehash...........


Title: Re: Print 000 As Numerical Variable
Post by: Ninkazu on August 03, 2009, 08:46:57 AM
Clippy. Chill pill.


Title: Re: Print 000 As Numerical Variable
Post by: Clippy on August 04, 2009, 09:00:07 PM
Thanks, I needed that pill, lol  :o