[Site Home] [Forum Home] [Articles] [File DB] [News Archives]

Programming -> Quiz program - elsifs and loops problems need help


(View original topic)


Lone Lobo - Nov-16-2006 server time
You have some major problems. The End If doesn't need to go directly after the if statement. It goes after all what should happen if the conditions of that if statement are "true".

Say:
if 1+1=2
then
put "1+1=2"
end if

if 1+1=3
then
put "1+1=3"
end if

1+1=2, obviously, so on the output screen you'd see "1+1=2". 1+1 doesn't equal 3, obviously, so all the code inside that if statement is skipped completely.

Also, about elsif:

if 1+1=2
then
put "Hi!"
elsif 2+2=4
put "Hello!"
end if

elsif needs to be nested inside an existing if statement, and it acts exactly as an else statement, and an if statement.

vahnx - May-19-2006 server time
You need a variable at the top of your program called answer: var answer : string

And why is your program all spaced out and stuff?

Instead of elsif, use if. Elsif's have to be inside an if statment, like follows:

if x = 1 then
put "hi"
elsif x = 2 then
put "hello"
end if

You cant have:

if x = 1 then
put "hi"
end if
elsif x = 2
put "hello"

Treflips - Apr-27-2006 server time
My end when things ,don't have a loop ontop of it do think that might be causing my problem?

Furball - Apr-27-2006 server time
QUOTE
I'm getting these but more like 9 messages.
elsif without matching if
syntax error at if expected end loop
exit can be used only in a loop ir for statement


Yup, your statements are either missing, or not constructed properly. Possible reasons is you might have an elsif statement that is outside a condition statement or does not have a parent if statement. Remember that in conditional statements, you cannot use elsif without a primary "if condition then" statement. The second error suggests that you closed your loop before you closed your if statement. You might have added them in, but they are in the wrong order. Also, exit when can only be used while you are inside a loop. Exit when is a special condition where you immediately exit the loop you are in, if the statement evaluates as being true. Exit when will not jump you out of conditional statements.

Treflips - Apr-27-2006 server time
I'm getting these but more like 9 messages.
elsif without matching if
syntax error at if expected end loop
exit can be used only in a loop ir for statement

Furball - Apr-27-2006 server time
What error is the program giving you?

Treflips - Apr-27-2006 server time
I have no idea where I'm missing the closing statements, after I add that in and take out the extra end if will my program run? BTW thanks for the help.

Furball - Apr-27-2006 server time
That's a bit better, but you're still missing some closing statements

CODE

if answer = "1" then
                              end if  
                                put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"
                              elsif answer = "2" then
                              end if  
                               put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                              elsif answer = "3" then
                              end if  
                               put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                              elsif answer = "4" then
                              end if    
                              put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"


Your conditional statement is constructed incorrectly. It should be like this:

if condition then
elsif condition then
end if

The way elsif works is it's a part of the same conditional statement. Seeing as it's part of the same statement, you don't need to end if each elsif statement (In this language at least). Think of it this way:

if condition then
%
%comments....to show spacing
%
%
elsif condition2 then
%
%
%
%
elsif condition3 then
%
%
%
%
end if

See how I only used one end if for that whole statement? This is due to the fact that elsifs are sub-conditions to the "main" condition. If your first condition fails, it checks your next elsif, if that fails, checks the next, and so on.

Your program uses alot of loop and conditional nesting (loops inside loops inside loops, or if's inside if statements inside if statements) which can be a good thing, but the more nested your program becomes, the more complex it can be to read (especially if there's an error somewhere).

Also, make sure at the end of your program, you close your loops. Same idea goes for loops:

loop
%
%Content in here <---
%
end loop

for each loop, you need to close it, just like if statements.

Treflips - Apr-27-2006 server time
Okay, how about I fill the end if and then tell me what am I still doing wrong because I did that before and still didn't run.
CODE
loop

   for column : 1 .. 80
       locate (1, column)
       put "*" ..
   end for

   for row : 2 .. 16
       locate (row, 1)
       put "*" ..
       locate (row, 80)
       put "*" ..
   end for

   for column : 1 .. 80
       locate (17, column)
       put "*" ..
   end for

   for row : 18 .. 24
       locate (row, 1)
       put "*" ..
       locate (row, 80)
       put "*" ..
   end for

   for column : 1 .. 80
       locate (25, column)
       put "*" ..
   end for



loop
   color (2)
   put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
   get answer
   exit when answer = "yes"
   if answer = "no" then
end if
       put " "
       color (blue)
       put " Do you use the term NOOB?  yes or no"

       get answer
       exit when answer = "no"
       if answer = "yes" then
end if



           put " When you get a headshot kill in a first person shoot, do you say BOOOOM HEADSHOT? Yes or No "
           get answer
           exit when answer = "No"
           if answer = "yes" then
           end if    
           put " When you here the word camping do you instantly thinking about campers in a FPS? Yes or No"
               get answer
               exit when answer = "No"
               if answer = "Yes" then

               end if  
                put " The first thing that comes to your mind when you here CS is.. (1) Computer Science or (2) Counter-Strike"
                   get answer
                   exit when answer = "1"
                   if answer = "2" then
                   end if    
                   put " Do you run around your house  pretending you're hold a guy and make gun sounds and say I can dance all day I can dance all day try hitting him noob? Yes or No"
                       get answer
                       exit when answer = " No"
                       if answer = "Yes" then
                         
                       end if put " When you see a gun in a movie can you recognize it right away?  yes or no"
                           color (red)

                           get answer
                           exit when answer = "no"
                           if answer = "yes" then


                            end if




                              %Ask the 7th question
                               color (blue)
                               put " "
                               put "Your third question is.."
                               put "which of these reason do you think is the best reason you play FPS? Choose from 1 to 5 "
                               color (100)
                               put
                                   "(1)Hockey not violent and bloody enough (2)Not enough people shooting at you on slight in real life (3) I just want to blow something up (4)Already got kicked out of the real army (5) I'm just bored and want to play something : "
..
                               get answer
                               exit when answer = "(5) I'm bored and want to play something"
                               if answer = "1" then
                               end if  
                                 put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"
                               elsif answer = "2" then
                               end if  
                                put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                               elsif answer = "3" then
                               end if  
                                put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                               elsif answer = "4" then
                               end if    
                               put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"


                                   color (red)
                                   get answer
                                   if answer = " World War 2" then
                                   end if    
                                   put " Your battlefield game is Battlefield 1942 and you played the game? Yes or No"
                                       get answer
                                       exit when answer = " No"
                                       if answer = "Yes" then
                                       end if    
                                       put " If you have made it this far you are 100% addicted to first person shooters"
                                           loop
                                               color (2)
                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                               get answer
                                               exit when answer = "no"
                                               if answer = "Yes" then
                                               end if    
                                               put " Now don't you feel better for telling the truth?"
                                                   if answer = " Vietnam War" then
                                                     
                                                   end if put " Your Battlefield game is Battleifield Vietnam have you play this game? Yes or No"
                                                       get answer
                                                       exit when answer = " No"
                                                       if answer = "Yes" then
                                                           put " If you have made it this far you are 100% addicted to first person shooters"
                                                           end if
                                                           loop
                                                               color (2)
                                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                                               get answer
                                                               exit when answer = "no"
                                                               if answer = "Yes" then
                                                                 
                                                               end if put " Now don't you feel better for telling the truth?"
                                                                   if answer = "Iraq War" then
                                                                       put " Your Battlefield game is Battlefield 2 have you played this game? Yes or No "
                                                                       get answer
                                                                       exit when answer = " No"
                                                                       if answer = "Yes" then
                                                                       end if    
                                                                       put " If you have made it this far you are 100% addicted to first person shooters"
                                                                           loop
                                                                               color (2)
                                                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                                                               get answer
                                                                               exit when answer = "no"
                                                                               if answer = "Yes" then
                                                                                   put " Now don't you feel better for telling the truth?"
                                                                               end if
                                                                   
                                                                               
end loop


Furball - Apr-27-2006 server time
Sorry, I don't rewrite other people's coding. You'll learn the language better if you code it yourself. Debugging is a part of programming and even though it can be a pain in the ass, you learn from your mistakes. What I suggest to do is attempt at closing those opened statements. Make a count of how many IF statements you have, write them down, and while reading your program, if you come across the place where your statement should have ended, insert your "end if" statement and scratch that number off your list. If that doesn't work, you can try commenting out the portion of your program that's causing problems and concentrate on creating the conditions before adding the content into your statements. Example

If condition then

if condition 2 then
end if

if condition 3 then
else if condition 4 then
end if

end if

Create the bones of the statement then you can just copy and paste your content in between afterwards. Hope this helps.

Treflips - Apr-26-2006 server time
If possible can you do it for me please and post it back here, I alreayd tried putting end if after everytime I use an if statement and still it doesn't work. pressanykey.gif
edit: or you could just tell me the excat place to put end if.

Furball - Apr-26-2006 server time
You need to close your conditional statements. You left at least 10 of them opened. Whenever you open a statement such as:

If condition then


you need to close it with

end if

same idea with loops. If you don't close them, you'll get a syntax error and you'll have a hell of a time finding which statement you forgot to close. Best programming practice is to write the bare bones of your statements before you fill them with code:

Example

If (your condition here) then
end if

then you don't have to worry about syntax errors due to forgetting to close your statements.

Treflips - Apr-26-2006 server time
Okay my program doesn't run can someone help me?
CODE
loop
   color (2)
   put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
   get answer
   exit when answer = "yes"
   if answer = "no" then

       put " "
       color (blue)
       put " Do you use the term NOOB?  yes or no"

       get answer
       exit when answer = "no"
       if answer = "yes" then




           put " When you get a headshot kill in a first person shoot, do you say BOOOOM HEADSHOT? Yes or No "
           get answer
           exit when answer = "No"
           if answer = "yes" then
               put " When you here the word camping do you instantly thinking about campers in a FPS? Yes or No"
               get answer
               exit when answer = "No"
               if answer = "Yes" then

                   put " The first thing that comes to your mind when you here CS is.. (1) Computer Science or (2) Counter-Strike"
                   get answer
                   exit when answer = "1"
                   if answer = "2" then
                       put " Do you run around your house  pretending you're hold a guy and make gun sounds and say I can dance all day I can dance all day try hitting him noob? Yes or No"
                       get answer
                       exit when answer = " No"
                       if answer = "Yes" then
                           put " When you see a gun in a movie can you recognize it right away?  yes or no"
                           color (red)

                           get answer
                           exit when answer = "no"
                           if answer = "yes" then







                               %Ask the 7th question
                               color (blue)
                               put " "
                               put "Your third question is.."
                               put "which of these reason do you think is the best reason you play FPS? Choose from 1 to 5 "
                               color (100)
                               put
                                   "(1)Hockey not violent and bloody enough (2)Not enough people shooting at you on slight in real life (3) I just want to blow something up (4)Already got kicked out of the real army (5) I'm just bored and want to play something : "
..
                               get answer
                               exit when answer = "(5) I'm bored and want to play something"
                               if answer = "1" then
                                   put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"
                               elsif answer = "2" then
                                   put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                               elsif answer = "3" then
                                   put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"

                               elsif answer = "4" then
                                   put "What War Ear Would You Rather Be A Part Of? World War 2 or Vietnam War or Iraq War"


                                   color (red)
                                   get answer
                                   if answer = " World War 2" then
                                       put " Your battlefield game is Battlefield 1942 and you played the game? Yes or No"
                                       get answer
                                       exit when answer = " No"
                                       if answer = "Yes" then
                                           put " If you have made it this far you are 100% addicted to first person shooters"
                                           loop
                                               color (2)
                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                               get answer
                                               exit when answer = "no"
                                               if answer = "Yes" then
                                                   put " Now don't you feel better for telling the truth?"
                                                   if answer = " Vietnam War" then
                                                       put " Your Battlefield game is Battleifield Vietnam have you play this game? Yes or No"
                                                       get answer
                                                       exit when answer = " No"
                                                       if answer = "Yes" then
                                                           put " If you have made it this far you are 100% addicted to first person shooters"
                                                           loop
                                                               color (2)
                                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                                               get answer
                                                               exit when answer = "no"
                                                               if answer = "Yes" then
                                                                   put " Now don't you feel better for telling the truth?"
                                                                   if answer = "Iraq War" then
                                                                       put " Your Battlefield game is Battlefield 2 have you played this game? Yes or No "
                                                                       get answer
                                                                       exit when answer = " No"
                                                                       if answer = "Yes" then
                                                                           put " If you have made it this far you are 100% addicted to first person shooters"
                                                                           loop
                                                                               color (2)
                                                                               put "Are You Addiicted To First Person Shooters ?? yes or no: " ..
                                                                               get answer
                                                                               exit when answer = "no"
                                                                               if answer = "Yes" then
                                                                                   put " Now don't you feel better for telling the truth?"
                                                                               end if
                                                                           end loop

Can someone fix it up and post it back here please? em194.gif

(Showing 50 last posts, newest on top)