-3

I'm trying to make a system which checks for money before letting you buy something. I'm stuck on the error: "5 was unexpected at this time." I don't know what I'm doing wrong and would love some help.

Code:

:711Beans
title BEANS AAAAAAA
if %money% LSS 5 (
    title No Money?
    cls
    echo Silly, you don't have enough money to buy beans.
    echo Cash Money: $%money%
    pause
    goto 711
)
if %money% GEQ 5 (
    cls
    set /a money=%money%-5
    echo You buy somes fromt he 7/11 for 5$
    echo Cash Money: $%money%
    pause
    goto 711
)

My expected result is the system checks for money, if it's less than a certain number, it doesn't let me buy beans, or if I have sufficient money, it lets me buy beans. However, my actual result is "5 was unexpected at this time."

Suraj Rao
  • 28,850
  • 10
  • 94
  • 99
  • What is money set to? Where is the label 711? – Anon Coward May 08 '22 at 04:51
  • 2
    The logical condition of an `if` statement is `if thing1 operator thing2` . Both `thing1` and `thing2` are strings and must **BOTH** be "quoted" if **Either** contains spaces or other separators. If both are integers <2^32 then the comparison will be performed arithmetically. The operator is one of a fixed set (`lss` and `geq` you know). If `thing1` has not been defined, then the `if` statement interpreted by `cmd` is `if operator thing2` - and `cmd` objects to `thing2` not being an operator, hence the message. – Magoo May 08 '22 at 05:29
  • 2
    @Magoo. Not 100% correct with the quoted `if` values where `geq`, `lss` etc is concerned. Try this for yourself: `if "100" lss "20" echo 100 lss 20` compared to `if 100 lss 20 echo 100 lss 20`. – Gerhard May 08 '22 at 06:32
  • 2
    @Windows_736. You must be setting `%money%` incorrectly, so you end up doing `if LSS 5 (` and you will get this exact error. Are there spaces before in your `set` command? it should be `set "money=n"` with no spaces, else that variable never exists as `%money%` but instead it will be something like `%money %` (note the trailing whitespace. – Gerhard May 08 '22 at 06:40
  • 2
    @Gerhard : absolutely correct. `"100"` is a string. It is not an integer. `100` is also a string, as are all batch variables, but it's also an integer as it contains only numeric characters. – Magoo May 08 '22 at 06:52
  • 1
    So, like I have stated in my comment, using `"` around the variables will match the values as strings and not integers. if `set /p money=` So instead of enclosing your `if` statements with double quotes, you can have a statement prior to your if conditions as `if not defined money echo 'money' cannot be empty & goto :EOF` where you can swop `:EOF` to any other label such as `:begin` if that label exists in the start of the script. – Gerhard May 08 '22 at 07:50

0 Answers0