cannot maintain variables in the subs

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
very confused

cannot maintain variables in the subs

Post by very confused »

I am creating a program in QBASIC and am stuck. Here is my program:
>
> DECLARE SUB ending ()
> DECLARE SUB menu ()
>
> CLS
> PRINT "Please select from the menu below any thing you wish to order (type the PRICE of the item you wish to order)"
> PRINT "An externel Floppy drive 46.99$"
> PRINT "An external CD-ROM drive 72.55$"
> PRINT "An external DVD-ROM drive 99.99$"
> PRINT "An external CD/DVD-ROM combo burner drive 255.68$"
> PRINT "50 blank 80 minute CD-ROMs 25.32$"
> PRINT "50 blank 100 minute CD-ROMs 32.25$"
> PRINT "50 blank 100 minute DVDs 100.00$"
> PRINT "50 blank floppies 25.55$"
> PRINT "300 sheets of recycled computer paper 2.99$"
> PRINT "300 sheets of computer cardstock 4.99$"
> PRINT "300 sheets of bright computer paper 3.50$"
> INPUT order%
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order2%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount2%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order3%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount3%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order4%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount4%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order5%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount5%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order6%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount6%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order7%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount7%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order8%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount8%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order9%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount9%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order10%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount10%
> PRINT "Would you like to order anything else?"
> INPUT wouldyou$
> IF wouldyou$ = "y" OR wouldyou$ = "Y" OR wouldyou$ = "yes" THEN menu
> INPUT order11%
>
> CLS
> PRINT "How much would you like (do not spell out numbers)"
> INPUT amount11%
> price% = order% * amount%
> price2% = order2% * amount2%
> price3% = order3% * amount3%
> price4% = order4% * amount4%
> price5% = order5% * amount5%
> price6% = order6% * amount6%
> price7% = order7% * amount7%
> price8% = order8% * amount8%
> price9% = order9% * amount9%
> price10% = order10% * amount10%
> price11% = order11% * amount11%
> subtotal% = price% + price2% + price3% + price4% + price5% + price6% +
> price7% + price8% + price9% + price10% + price11% PRINT "total of this
> order "; price%
>
> PRINT "total of this order "; price2%
>
> PRINT "total of this order "; price3%
>
> PRINT "total of this order "; price4%
>
> PRINT "total of this order "; price5%
>
> PRINT "total of this order "; price6%
>
> PRINT "total of this order "; price7%
>
> PRINT "total of this order "; price8%
>
> PRINT "total of this order "; price9%
>
> PRINT "total of this order "; price10%
>
> PRINT "total of this order "; price11% PRINT PRINT "Your subtotal
> comes to:"
> PRINT subtotal%
> tax% = subtotal% * .0875
> PRINT "Your tax comes to:"
> PRINT tax%
> total% = subtotal% + tax%
> PRINT "Your total comes to:"
> PRINT total%
> PRINT "Press enter to continue"
> INPUT enter$
>
> CLS
> PRINT "Hello my name is compu. I am the computerized version of our buisness. I take over after you have made your order."
> SLEEP 25
> CLS
> PRINT "What is your full first name"
> INPUT name$
> SLEEP 1
> DO
> CLS
> PRINT "Hello "; name$; "!!!"
> SLEEP 2
> CLS
> PRINT name$; "You must take a short survey in order to purchase this product so we may have your information on file to send the product to you. Do you wish to continue"
> INPUT survey$
> IF survey$ = "N" OR survey$ = "n" OR survey$ = "no" THEN END
> CLS
> SLEEP 2
> PRINT "What is your middle name"
> INPUT middle$
> CLS
> SLEEP 1
> PRINT "What is your last name"
> INPUT last$
> CLS
> SLEEP 1
> PRINT "What is your address"
> INPUT address$
> CLS
> SLEEP 1
> PRINT "What city do you live in"
> INPUT city$
> CLS
> SLEEP 1
> PRINT "What state do you live in"
> INPUT state$
> CLS
> SLEEP 1
> PRINT "What is your zipcode"
> INPUT zipcode
> CLS
> SLEEP 1
> PRINT "What is your current age in years"
> INPUT years%
> CLS
> SLEEP 1
> PRINT "We are now processing your entry. Your cooperation has been appriciated. Please be patient."
> SLEEP 6
> CLS
> PRINT "Please check your information below:"
> PRINT "Your full name is "; name$; " "; middle$; " "; last$
> PRINT "You live at "
> PRINT ; address$
> PRINT city$; ", "; state$; " "; zipcode
> months% = years% * 12
> days% = years% * 365
> PRINT "You are "; years%; " years old"
> PRINT "You are "; months%; " months old"
> PRINT "You are "; days%; " days old"
> PRINT "Is this correct? (Y or N)"
> INPUT Answer$
> LOOP UNTIL Answer$ = "Y" OR Answer$ = "y" OR Answer$ = "yes"
> CLS
> PRINT "Thank you for your cooperation in the signing of our optional hit list, "; name$; "."
> PRINT "Please allow 24 to 36 hours for the processing of the hitman which we will send to your house."
> SLEEP 17
> CLS
> SCREEN 7
> CIRCLE (100, 100), 50, 14
> PSET (80, 80), 1
> PSET (120, 80), 1
> PSET (100, 100), 6
> LINE (75, 110)-(85, 125), 4
> LINE (85, 125)-(100, 130), 4
> LINE (100, 130)-(115, 125), 4
> LINE (115, 125)-(125, 110), 4
> SOUND 261.63, (1 * 5)
> SOUND 293.66, (1 * 5)
> SOUND 329.63, (1 * 5)
> SOUND 349.23, (1 * 15)
> SOUND 261.63, (1 * 5)
> SOUND 293.66, (1 * 5)
> SOUND 329.63, (1 * 5)
> SOUND 349.23, (1 * 20)
> SOUND 329.63, (1 * 5)
> SOUND 293.66, (1 * 5)
> SOUND 261.63, (1 * 15)
> SOUND 261.63, (1 * 10)
> SOUND 293.66, (1 * 10)
> SOUND 329.63, (1 * 10)
> SOUND 349.23, (1 * 10)
> PRINT "Thank you and have a wonderful day "; name$; "!!!"
>
> SUB ending
> price% = order% * amount%
> price2% = order2% * amount2%
> price3% = order3% * amount3%
> price4% = order4% * amount4%
> price5% = order5% * amount5%
> price6% = order6% * amount6%
> price7% = order7% * amount7%
> price8% = order8% * amount8%
> price9% = order9% * amount9%
> price10% = order10% * amount10%
> price11% = order11% * amount11%
> subtotal% = price% + price2% + price3% + price4% + price5% + price6% +
> price7% + price8% + price9% + price10% + price11% PRINT "total of this
> order "; price%
>
> PRINT "total of this order "; price2%
>
> PRINT "total of this order "; price3%
>
> PRINT "total of this order "; price4%
>
> PRINT "total of this order "; price5%
>
> PRINT "total of this order "; price6%
>
> PRINT "total of this order "; price7%
>
> PRINT "total of this order "; price8%
>
> PRINT "total of this order "; price9%
>
> PRINT "total of this order "; price10%
>
> PRINT "total of this order "; price11% PRINT PRINT "Your subtotal
> comes to:"
> PRINT subtotal%
> tax% = subtotal% * .0875
> PRINT "Your tax comes to:"
> PRINT tax%
> total% = subtotal% + tax%
> PRINT "Your total comes to:"
> PRINT total%
> PRINT "Press enter to continue"
> INPUT enter$
>
> END SUB
>
> SUB menu
> CLS
> PRINT "Please select from the menu below any thing you wish to order (type the PRICE into the blank)"
> PRINT "An externel Floppy drive 46.99$"
> PRINT "An external CD-ROM drive 72.55$"
> PRINT "An external DVD-ROM drive 99.99$"
> PRINT "An external CD/DVD-ROM combo burner drive 255.68$"
> PRINT "50 blank 80 minute CD-ROMs 25.32$"
> PRINT "50 blank 100 minute CD-ROMs 32.25$"
> PRINT "50 blank 100 minute DVDs 100.00$"
> PRINT "50 blank floppies 25.55$"
> PRINT "300 sheets of recycled computer paper 2.99$"
> PRINT "300 sheets of computer cardstock 4.99$"
> PRINT "300 sheets of bright computer paper 3.50$"
>
> END SUB
>
> My problem is that I cannot use my prices in my sub. I am really confused!!!!! I need to carry over my information. HELP?
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

You need to use public or global variables:

COMMON SHARED variable% 'shares across entire application

or

DIM SHARED variable% 'shares across entire module

and next time, try not to make it so obvious that you're copying someone else's email. :D
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Also, this is a MUCH better way of doing menus for noobs.

Code: Select all

DEFINT A-Z
CLS

TotalChoices = 3
PlayerPlace = 1

DO

PRINT " Player Game" 'Notice the space
PRINT " Help"
PRINT " Quit"

A$ = INKEY$

IF A$ = "" THEN
 DO
 A$ = INKEY$        'Make sure A$ has something
  LOOP UNTIL A$ <> ""
ENDIF

' There is an ASCII char, but I dont know it. For an arrow I will use a )

IF UCASE$(A$) = "I" AND PlayerPlace <> TotalChoices THEN PlayerPlace = PlayerPlace - 1
IF UCASE$(A$) = "K" AND PlayerPlace <> 1 THEN PlayerPlace = PlayerPlace + 1

LOCATE 0, PlayerChoice
PRINT ")"
LOOP UNTIL A$ = " "
Or, what I sometimes do is put the choices in an array and use UBOUND and LBOUND to get the maximum stuff, CODE IS UNTESTED!!! (Should work though)
Image
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

HOLY SH**!!!! :shock:
I haven't seen this bad programming since I first started out.
Making a program like that is VERY limited and VERY time
consuming.
PLEASE do your self a favor and learn the blessings
of ARRAYS and LOOPS.
Also NEVER use GOTO, even though you didn't use it
NEVER use it in your programs. It gets very complicated to
debug and is "stringy". I put this in here because noobs
have a habit of using those.
You cannot write good programs until you learn the
essentails, specificaly Loops and Arrays. There are also
many other commands that are necessy but thats why
there is a help file. I'm sorry if this message appeared to be
cutting you down, which there was no intent, but you REALLY need to learn these things fast. Also I have this strange feeling that you are learning
QB in a class...

-Mit
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Elitist bastards: Cut the new people some slack, will you.

I use GOTO in my code, I'm not a noob.




Not that it matters as this topic is about a month old.
I have left this dump.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

:wink: I used GOTO over 126 times in Robo Raider(Classic style), I also used few other statements over 126 times,.. some I could have made a CALL for so not to use so many, but GOTO is what controls the door codes in the mazes, and also that annoying crash code,... annoying, what am I saying, I wrote that, oh sorry, talking 2 meself, hehe!

Also the Batt. code, that one was nice though, gives you 30mins, or it ment to, goes off LOOP speed.. can't remember how many LOOP per second 7.1 made,. sorry it wont load into QB 1.1, it'd give you more time, its LOOP speed is slow.

:roll: I'm chaining Space Warp though, making sure I can compile that 1 :!: :) .
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Hi,
As I said before I was not trying to be harsh on the guy,
just that his life could be SOOOOO much easier.
Also GOTO is a habit you should get out of. Use
Do LOOPs to compensate instead because GOTO is
really hard to debug and not considered a
good way to code.

-Mit

P.S. I never ment that you were a noob Z!re
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Read my other response about GOTO. But judging someone's coding technique is not our responsibility, nor is it ever wanted or even called for. Everyone codes differently, it's about as unique as our fingerprints. Judging someone's coding style is immoral enough, but outright insulting it is cause for major flames. I hope you brought a pair of asbestos underpants, coz if you keep going this way, you're gonna need 'em in short order. :D
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Yeah Nekro,
Your right I shouldn't be judging somebody else's
programing techniques, I guess I was a little pissed
yesterday about some Lambert thing... :oops:
But I still hold to my arguement that
GOTO is stupid and shouldn't have even been included
in QB. :|
-Mit
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

:shock: How is GOTO so bad, its like a miny CALL, and I can't see why its not useful is some aplications. I mean its great in different ways, and yeah in some its not. But all??? :?:

:roll: Oh well..... :)
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Ya know what I'm just going to drop my vendeta about the GOTO thing.
It started as a little advice but now I've probaly got the hate of
every programmer in this community :cry:
See I was just trying to help but I was no help *sigh*
So no more from me about the GOTOs :D!

-Mit
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Aye, you'd have to do more than complain about GOTO's to cause ne level of hate n me, and then after that, I hate very little of anything, its a waste of time to go round hating.

As for the others, they seem nice, I havn't crossed ne yet, n tg they havn't crossed me (which is also hard 2 do, although I can get fired up easily :wink: )

Oh well, I use GOTO, n u don't, we'll leave it there, :wink:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

*Puts on BASIC-Combat suit*
*Arms PRINT-Rifle*
*Equips ?GL-Grenade*
*Calls in GOTO-Chopper using SOUND device*

-Take me to Mitth'raw'nuruodo
-Yes, sir!

*Makes evil grin as I think of the pain that await Mitth'raw'nuruodo while the GOTO-Chopper takes off*
I have left this dump.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

:shock: Ok, I'll take back that; "..All the others seem nice..", Z!re has got some heavy equip. it seems!!! :wink: (Gets in DATA bot, fires up GET ray and captures the GOTO copper) LETs not fight now, sure we can work this out??
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Look out, Z!re's GOTO coptor is protected by '$DYNAMIC force fields and is armed with CVSMBF missles. You might not wanna mess with him, especially when his backup team of CHAIN warriors LOCATE you in the FIELD with their GOSUB scanners and DECLARE you NEXT for the END. :lol:
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

:) :lol: :D :lol: :)



It's a shame that those stories were lost...
Silly TBN crash :cry:
I have left this dump.
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Ya, thanks. 8)
I just like QB a lot and hate to see people use stuff they don't
need to use, but that's just me...
Oh yea and thanks Z!re for that wonderful picture that
will scar me for the rest of my life... :D
But I have worst, sorry Z!re. :lol:
-Mit
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Give it up.

Goto is there for a reason, it can be found in almost any language.

And no matter what you do, you'll end up using "goto" anyways, in the form of jmp in ASM


A goto can shorten your code dramatically, example:

Code: Select all

Flag = 0
For a = 0 To 1000
 For b = 0 To 1000
  For c = 0 To 1000
   For d = 0 To 1000
    If a = 5 And b = 10 And c = 833 And d = 162 Then Flag = 1
    If Flag = 1 Then Exit For
   Next
   If Flag = 1 Then Exit For
  Next
  If Flag = 1 Then Exit For
 Next
 If Flag = 1 Then Exit For
Next
If Flag = 0 Then
 Print "ERROR"
 End
End If
Versus:

Code: Select all

For a = 0 To 1000
 For b = 0 To 1000
  For c = 0 To 1000
   For d = 0 To 1000
    If a = 5 And b = 10 And c = 833 And d = 162 Then Goto EndNest
   Next
  Next
 Next
Next
Print "ERROR"
End
EndNest:
Should you or me count lines/readability?

Can't you just accept the fact that people code differently?

For example I never comment my code, ever. Everyone says it's bad coding, so?, I don't care, I don't need any comments to remember what my code does, and if I share it in a multi-programmer project, I explain if they don't get it. So far, they have understood without any problems. Oh, and I use GOTO.
I have left this dump.
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Ok Z!re, I think I already established that I understood
the benifits of GOTO, and was not going to comment
anymore about people's bad habits. That said I
DID give it up and you are the one who just brought it back.
I want to make it clear now that I was sorry to critizise,
understand that GOTOs could be good, and don't want to
hear about the GOTO thing ever again, PLEASE give me a
break! Just don't expect to see any GOTOs in my programs :wink: .

-Mit
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Oh yah.. so you think you can just establish stuff now ey!

Well, I tell you what!, Around here we don't take kindly to folks going around Establishing things! Ya hear that!

Tar and feathers, Tar and feathers, TAR AND FEATHERS! :lol:
I have left this dump.
Post Reply