Qbasicnews.com

Full Version: Mishap in deducting values...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ok take this bit of code here:

[syntax="qbasic"]
choice$ = LCASE$(INPUT$(1))
IF choice$ = "v" THEN item = item + 40 AND money! = money! - 2.25
IF money! < 0 THEN item = item - 40 AND money! = money! + 2.25 AND qq = 1
IF qq = 1 THEN GOTO 88
[/syntax]
basicly what this is is if i press "v" then the item get's a boost of 40.
but if i can't afford it, it sends me to line "88" where then it will tell me 'i can't buy that..." yada yada yada, but the problem is that when i do press "v" item = 0 for some reason.
The keyword AND is actually a boolean operator. You can't use it like the english word and.

It would be better to use an IF...END IF block...

Like this:


Code:
choice$ = LCASE$(INPUT$(1))

IF choice$ = "v" THEN
   item = item + 40
   money! = money! - 2.25
END IF


IF money! < 0 THEN
   item = item - 40
   money! = money! + 2.25
   qq = 1
   GOTO 88
END IF

Good luck!
Wink
Replacing the AND words by a colon ":" should work fine.
Dio,
[syntax="qbasic"]
choice$ = LCASE$(INPUT$(1))

IF choice$ = "v" THEN
item = item + 40
money! = money! - 2.25
END IF

IF money! < 0 THEN
item = item - 40
money! = money! + 2.25
qq = 1
END IF

IF qq = 1 THEN GOTO 88
[/syntax]

or

[syntax="qbasic"]
choice$ = LCASE$(INPUT$(1))

IF choice$ = "v" THEN item = item + 40 : money! = money! - 2.25

IF money! < 0 THEN item = item - 40 : money! = money! + 2.25 : qq = 1
IF qq = 1 THEN GOTO 88
[/syntax]

I prefer the previous over the latter.
oh of course! sometimes i way over complicate stuff. :roll:
thanks
but that entire thing is waaay bloated. try something smaller like this:


[syntax="qbasic"]choice$ = LCASE$(INPUT$(1))
IF choice$ = "v" THEN
IF money! > 0 THEN item = item + 40: money! = money! - 2.25 ELSE GOTO 88
END IF[/syntax]

That does exactly the same job. Smile
Nevermind, my bad..

[syntax="QBASIC"]'Code removed![/syntax]

... :wink:
Not meaning to interfere, but the code sample Rattrap posted is definetely not the same as the original.

Try this following ultra-simple example to see what's the difference:
Code:
money! = 1
v$ = "v"

IF v$ = "v" THEN
   money! = money! - 2.25
END IF

IF money! < 0 THEN
   money! = money! + 2.25
END IF

PRINT money!
Code:
money! = 1
v$ = "v"

IF v$ = "v" THEN
   money! = money! - 2.25
ELSEIF money! < 0 THEN
   money! = money! + 2.25
END IF

PRINT money!

So please beware that you can't always use the simplified form for an IF block using ELSEIF, it results in different execution of the code.
:o Nevermind my code,.. didn't see that.. Lucky I only use it for arrowkey scanning.. :wink:
I don't like to use colons unless it's a limited line challenge that allows it. You might as well use code that's easier to read. Wink