-1

I've been learning FreeBasic and started writing a well Basic text adventure game. I've been following the same formula for a while, but on my most recent development gained the following error:

beta.bas(226) error 32: Expected 'END IF' in 'end'

This is strange, as everything was working fine previously. Here are two copies of my code-

Broken-

Dim As String strInput

main:
    print "You have awakened in a dark room. What do you do? "
    input ">", strInput
    sleep 500

    if strinput = "Leave" then
        goto option1L1
    else if strinput = "leave" then
        goto option1L1
    else
        print "You can't do that."
    end if
    sleep 500
    goto main

option1L1:
    print "You have left the room"
    sleep 500
    print "You are now outside. What do you want to do.?"
    input ">", strInput
    sleep 500
    if strinput = "Look around" then 
        goto lookaround1
    else if strinput = "look around" then
        goto lookaround1
    else
        print "You can't do that."
    end if
    goto option1L1

lookaround1:
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?"
    sleep 500
    input ">", strinput
    if strinput = "Dark forest" then
        goto darkforest
    else if strinput = "dark forest" then
        goto darkforest
    else if strinput = "Dark Forest" then
        goto darkforest
    else if strinput = "Happy meadow" then
        goto happymeadow
    else if strinput = "Happy Meadow" then
        goto happymeadow
    else if strinput = "happy meadow" then
        goto happymeadow
    else if strinput = "happy looking meadow" then
        goto happymeadow
    else if strinput = "Happy looking meadow" then
        goto happymeadow
    else if strinput = "Happy Looking Meadow" then
        goto happymeadow
    else
        print "You can't do that."
    end if
    goto lookaround1
    end if
    end

darkforest:
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?"
    print "Options- 'Attack bear' or 'Run from bear'"
    print "Tip- Commands are case sensitive"
    sleep 500
    input ">", strinput
    if strinput = "Attack bear" then
        goto attackbear
    else if strinput = "Run from bear" then
        goto runfrombear
    else
        print "You can't do that."
    END if
    goto darkforest
    END

happymeadow:
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye."
    END if
    end

attackbear:
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?"
    print "Your options are to 'Make a splint' or to 'Press onward'"
    sleep 500
    input ">", strinput
    if strinput = "Make a splint" then
        goto makeasplint
    else if strinput = "Press onward" then
        goto pressonward
    else
        print "You can't do that."
    END if
    goto attackbear
    end if
    END

runfrombear:

    print "You tried to run from the bear, but were eaten. Sorry!"
    END if
    end

makeasplint:
    print "You made a splint, and are able to carry on."
    print "Travelling..."
    sleep 1000
    print "You have come to a small house. What do you do?"
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidehouse
    else if strinput = "Wait outside" then
        goto outsidehouse
    else if strinput = "Keep travelling" then
        goto keeptravelling
    else 
        print "You can't do that."
        print "Tip: Commands are case sensitive"
        goto makeasplint
    END if
    end

pressonward:
    print "You press onward, but trip on a rock and die."
    END if
    end

insidehouse:
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?"
    print "Options: 'Yes' or 'No'"
    input ">", strinput
    if strinput = "Yes" then
        goto yes
    else if strinput = "No" then
        goto no
    else
        print "Please enter either 'Yes' or 'No'"
        print "Remember, all commands are case sensitive. Sorry!"
        goto insidehouse

    end if
    end

outsidehouse:
    print "You wait around outside the huse. You eventually die of starvation."
    end if
    end

keeptravelling:
    print "You keep travelling and eventually find your way out of the forest. "
    print "Travelling..."
    sleep 2000
    print "You have now come to a giant castle. What do you want to do?"
    print "Your options are 'Go inside' and 'Leave'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidecastle
    else if strinput = "Leave" then
        goto leave
    else
        print "You can't do that"
        goto keeptravelling
    END if
    end

yes:
    print "You equip the sword, but slip and stab yourself with it. You are dead."
    end if
    end

no:
    print "You leave the house."
    goto makeasplint
    end if
    end


insidecastle:
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious."
    print "..."
    sleep 2000
    print "After you awake, you find your movement to be constricted by some sort of leather strap."
    print "..."
    sleep 2000
    print "You hear a sound. It is a rough voice saying 'This is my swamp..."
    print "The ogre kills you, and you are dead."
    end if
    end

leave:
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?"
    print "Your options are 'Sleep' and 'Don't sleep'"
    input ">", strinput
    if strinput = "Sleep" then
        goto sleeping
    else if strinput = "Don't sleep" then
        goto dontsleep
    else
        print "You can't do that."
        goto leave
    end if
    end

sleeping:
    print "You die in your sleep."
    end if
    end

dontsleep:
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!"
    sleep 5000
    print "Do you wish to exit?"
    input ">", strinput
    if strinput = "Yes" then
        goto exitgame
    if strinput = "No" then
        print "Too bad!"
        goto exitgame
    end if
    end
exitgame:
    end

And to be clear, I have attempted to add an "end if" before the "end" there, but it still gave me:

"expected 'end if' in 'end if'"

Last working-

Dim As String strInput

main:
    print "You have awakened in a dark room. What do you do? "
    input ">", strInput
    sleep 500

    if strinput = "Leave" then
        goto option1L1
    else if strinput = "leave" then
        goto option1L1
    else
        print "You can't do that."
    end if
    sleep 500
    goto main

option1L1:
    print "You have left the room"
    sleep 500
    print "You are now outside. What do you want to do.?"
    input ">", strInput
    sleep 500
    if strinput = "Look around" then 
        goto lookaround1
    else
        print "You can't do that."
    end if
    goto option1L1

lookaround1:
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?"
    sleep 500
    input ">", strinput
    if strinput = "Dark forest" then
        goto darkforest
    else if strinput = "Happy meadow" then
        goto happymeadow
    else
        print "You can't do that."
    end if
    goto lookaround1
    end


darkforest:
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?"
    print "Options- 'Attack bear' or 'Run from bear'"
    print "Tip- Commands are case sensitive"
    sleep 500
    input ">", strinput
    if strinput = "Attack bear" then
        goto attackbear
    else if strinput = "Run from bear" then
        goto runfrombear
    else
        print "You can't do that."
    END if
    goto darkforest
    END

happymeadow:
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye."
    END if
    end


attackbear:
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?"
    print "Your options are to 'Make a splint' or to 'Press onward'"
    sleep 500
    input ">", strinput
    if strinput = "Make a splint" then
        goto makeasplint
    else if strinput = "Press onward" then
        goto pressonward
    else
        print "You can't do that."
    END if
    goto attackbear
    END

runfrombear:

    print "You tried to run from the bear, but were eaten. Sorry!"
    END if
    end

makeasplint:
    print "You made a splint, and are able to carry on."
    print "Travelling..."
    sleep 1000
    print "You have come to a small house. What do you do?"
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidehouse
    else if strinput = "Wait outside" then
        goto outsidehouse
    else if strinput = "Keep travelling" then
        goto keeptravelling
    else 
        print "You can't do that."
        print "Tip: Commands are case sensitive"
        goto makeasplint
    END if
    end

pressonward:
    print "You press onward, but trip on a rock and die."
    END if
    end

insidehouse:
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?"
    print "Options: 'Yes' or 'No'"
    input ">", strinput
    if strinput = "Yes" then
        goto yes
    else if strinput = "No" then
        goto no
    else
        print "Please enter either 'Yes' or 'No'"
        print "Remember, all commands are case sensitive. Sorry!"
        goto insidehouse

    end if
    end

outsidehouse:
    print "You wait around outside the huse. You eventually die of starvation."
    end if
    end

keeptravelling:
    print "You keep travelling and eventually find your way out of the forest. "
    print "Travelling..."
    sleep 2000
    print "You have now come to a giant castle. What do you want to do?"
    print "Your options are 'Go inside' and 'Leave'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidecastle
    else if strinput = "Leave" then
        goto leave
    else
        print "You can't do that"
        goto keeptravelling
    END if
    end

yes:
    print "You equip the sword, but slip and stab yourself with it. You are dead."
    end if
    end

no:
    print "You leave the house."
    goto makeasplint
    end if
    end


insidecastle:
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious."
    print "..."
    sleep 2000
    print "After you awake, you find your movement to be constricted by some sort of leather strap."
    print "..."
    sleep 2000
    print "You hear a sound. It is a rough voice saying 'This is my swamp..."
    print "The ogre kills you, and you are dead."
    end if
    end

leave:
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?"
    print "Your options are 'Sleep' and 'Don't sleep'"
    input ">", strinput
    if strinput = "Sleep" then
        goto sleeping
    else if strinput = "Don't sleep" then
        goto dontsleep
    else
        print "You can't do that."
        goto leave
    end if
    end

sleeping:
    print "You die in your sleep."
    END if
    end

dontsleep:
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!"
    end if
    end

I am quite a newbie at all of this and am very stumped. What am I doing wrong?

shA.t
  • 16,580
  • 5
  • 54
  • 111
  • 1
    If you indent your code a little more, err, ... intelligently, you'll probably see your problem instantly. No offence intended. – paxdiablo May 11 '15 at 03:39
  • I think; When a `GOTO` calls and compiler goes out of an `IF` block you don't need any `END IF` to finish that `IF` block, you need an `END IF` just for finishing an `IF` block ;). – shA.t May 11 '15 at 05:54

3 Answers3

2

You are using a programming style that is outdated nowadays - e.g. you use a lot of jumps using GOTO. FreeBasic provides lots of better concepts to structure your code: Functions/Subs, User defined types, Classes, Inheritance, ...

Nevertheless, there are some basic errors in the code that cause the compilation error(s). A major one is that you use "Else If" instead of "ElseIf" (note the space character). Another one is that you use "End If" although you have not started an IF block using "If" or "ElseIf".

Here's a version of your code, that is syntactically correct (it will compile), but still uses bad code style (e.g. GOTO and labels). Additionally there are semantical and other issues. E.g. an END does not make sense directly after a GOTO as it will never be reached.

Dim As String strInput

main:
    print "You have awakened in a dark room. What do you do? "
    input ">", strInput
    sleep 500

    if strinput = "Leave" then
        goto option1L1
    ElseIf strinput = "leave" then
        goto option1L1
    else
        print "You can't do that."
    end if
    sleep 500
    goto main

option1L1:
    print "You have left the room"
    sleep 500
    print "You are now outside. What do you want to do.?"
    input ">", strInput
    sleep 500
    if strinput = "Look around" then 
        goto lookaround1
    ElseIf strinput = "look around" then
        goto lookaround1
    else
        print "You can't do that."
    end if
    goto option1L1

lookaround1:
    print "You see a road leading into a dark forest, and a road leading into a happy looking meadow. Which path do you take?"
    sleep 500
    input ">", strinput
    if strinput = "Dark forest" then
        goto darkforest
    ElseIf strinput = "dark forest" then
        goto darkforest
    ElseIf strinput = "Dark Forest" then
        goto darkforest
    ElseIf strinput = "Happy meadow" then
        goto happymeadow
    ElseIf strinput = "Happy Meadow" then
        goto happymeadow
    ElseIf strinput = "happy meadow" then
        goto happymeadow
    ElseIf strinput = "happy looking meadow" then
        goto happymeadow
    ElseIf strinput = "Happy looking meadow" then
        goto happymeadow
    ElseIf strinput = "Happy Looking Meadow" then
        goto happymeadow
    else
        print "You can't do that."
    end if
    goto lookaround1
    end

darkforest:
    print "You have taken the path leading into the dark forest. Before you, you see a bear. What do you do?"
    print "Options- 'Attack bear' or 'Run from bear'"
    print "Tip- Commands are case sensitive"
    sleep 500
    input ">", strinput
    if strinput = "Attack bear" then
        goto attackbear
    ElseIf strinput = "Run from bear" then
        goto runfrombear
    else
        print "You can't do that."
    END if
    goto darkforest
    END

happymeadow:
    print "The meadow turns out to be a trap. Unfortunately, you are caught in this trap and are now dead. Goodbye."
    end

attackbear:
    print "You attack the bear with your 'bare' hands. You prevail, but have broken your arm. What do you do?"
    print "Your options are to 'Make a splint' or to 'Press onward'"
    sleep 500
    input ">", strinput
    if strinput = "Make a splint" then
        goto makeasplint
    ElseIf strinput = "Press onward" then
        goto pressonward
    else
        print "You can't do that."
    END if
    goto attackbear
    END

runfrombear:
    print "You tried to run from the bear, but were eaten. Sorry!"
    end

makeasplint:
    print "You made a splint, and are able to carry on."
    print "Travelling..."
    sleep 1000
    print "You have come to a small house. What do you do?"
    print "Options- 'Go inside', 'Wait outside, 'Keep travelling'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidehouse
    ElseIf strinput = "Wait outside" then
        goto outsidehouse
    ElseIf strinput = "Keep travelling" then
        goto keeptravelling
    else 
        print "You can't do that."
        print "Tip: Commands are case sensitive"
        goto makeasplint
    END if
    end

pressonward:
    print "You press onward, but trip on a rock and die."
    end

insidehouse:
    print "You have entered the cabin. Inside you find a sword. Do you want to equip the sword?"
    print "Options: 'Yes' or 'No'"
    input ">", strinput
    if strinput = "Yes" then
        goto yes
    ElseIf strinput = "No" then
        goto no
    else
        print "Please enter either 'Yes' or 'No'"
        print "Remember, all commands are case sensitive. Sorry!"
        goto insidehouse
    end if
    end

outsidehouse:
    print "You wait around outside the huse. You eventually die of starvation."
    end

keeptravelling:
    print "You keep travelling and eventually find your way out of the forest. "
    print "Travelling..."
    sleep 2000
    print "You have now come to a giant castle. What do you want to do?"
    print "Your options are 'Go inside' and 'Leave'"
    input ">", strinput
    if strinput = "Go inside" then
        goto insidecastle
    ElseIf strinput = "Leave" then
        goto leave
    else
        print "You can't do that"
        goto keeptravelling
    END if
    end

yes:
    print "You equip the sword, but slip and stab yourself with it. You are dead."
    end

no:
    print "You leave the house."
    goto makeasplint
    end


insidecastle:
    print "After entering the castle, you slip on a stone and hit your face on the floor, rendering you unconscious."
    print "..."
    sleep 2000
    print "After you awake, you find your movement to be constricted by some sort of leather strap."
    print "..."
    sleep 2000
    print "You hear a sound. It is a rough voice saying 'This is my swamp..."
    print "The ogre kills you, and you are dead."
    end

leave:
    print "You leave the castle, and find your way to a happy cottage, where you find a bed. What do you do now?"
    print "Your options are 'Sleep' and 'Don't sleep'"
    input ">", strinput
    if strinput = "Sleep" then
        goto sleeping
    ElseIf strinput = "Don't sleep" then
        goto dontsleep
    else
        print "You can't do that."
        goto leave
    end if
    end

sleeping:
    print "You die in your sleep."
    end

dontsleep:
    print "By not sleeping, you avoided the ghosts. Congrats, you've survived!"
    sleep 5000
    print "Do you wish to exit?"
    input ">", strinput
    if strinput = "Yes" then
        goto exitgame
    ElseIf strinput = "No" then
        print "Too bad!"
        goto exitgame
    end if
    end

exitgame:
    end
StW
  • 243
  • 2
  • 8
1

I am quite a newbie at all of this and am very stumped. What am I doing wrong?

Honestly, scrap your code and start again. BASIC is not by any metric a modern language, and it will teach you bad habits that will limit you when you try to work with a modern language - for example your prolific use of gotos which are in essence shunned nowadays, and have been since the mid 70s.

0

Use CASES instead of ElseIf

You can use the Arrays for store more than one choice or message inside a var


Dim as integer i = 0
dim as String Message(3)

Message(1) = "Hello"
Message(2) = "Middle Answer"
Message(3) = "End Answer"


   For i  = 1 To 16  Step 1 ' Init loop until 16 cicles
   print i

        Select Case as const i
            Case 1 
                Print "The message is:" & Message(i) ' The first 3 steps 
            Case 2 
                Print "The message is:" & Message(i) ' The first 3 steps 
            Case 3 
                Print "The message is:" & Message(i) ' The first 3 steps 
            Case 4    ' If i  = 4 Give this solution
                Print " Solution 1"
            Case  8    ' Same when = 8
                Print "Middle"
            Case 16   ' At last = 16
                Print "Last"
        End Select

    next i

As you can see, you can enter many more conditioned choices using CASE, also for a text adventure it is necessary to create set variables conditioned by the choices:

Dim AnswerPrompt As string
Dim MyStep as Integer
Dim MyGold as Integer 

'For a correct developing You Have to set each var before you use it

Dim as Integer MySword = 0 'Directly when you set

AnswerPrompt = "" 'Before the program ask
MyStep = 1
MyGold = 15

Line Input "Do you want give 10 gold for the Sword ?"; AnswerPrompt
If UCASE(AnswerPrompt) = "YES" Then ' UCASE transform all letter in capital
   If MyGold >= 10  Then 
      MyStep = MyStep + 5
      MyGold = MyGold - 10
      MySword = 1
    Else
      Print "Your Gold is not Enough"
    End If
Else
Print "Allright lets go away"
End If

Although FreeBASIC inherited GOTO from the old BASIC for compatibility, it is good practice to create a structure that does not require the use of GOTO, creating loops with Do While Until Loop and using previously specified functions where they carry out the task.

ExagonX
  • 141
  • 10