Computer Science Canada

help with if statement.

Author:  netninja [ Wed Feb 11, 2004 2:40 pm ]
Post subject:  help with if statement.

Help with this please. At the get song part, it says its a bad get statement or something, and I dont understand why. Also, it says sonne (at the top) isnt declared, which i also dont understand. Obviously the program isnt finished, its just the beggining and i dont understand some things that I would like to learn in order to finish it. Of course it will only take half a minute, but im just practicing basics so that i can reach my final destination of making a duckhunt game hehe. Anyway, Im very noob to turing, so i appreciate all help with my errors.



setscreen ("graphics")

var song: array 1..6 of string:=init (sonne, zwitter, bodies, buckdich, ichwill, duhast)

put "sonne"
put "zwitter"
put "bodies"
put "buckdich"
put "ichwill"
put "duhast"
put ""
put "Enter the song you wish to play:"

get song

if song = sonne then
Music.PlayFileLoop ("music/sonne.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

I also tried by making each song an integer instead of string, but that also didnt work. Laughing

Author:  Zorg [ Wed Feb 11, 2004 3:27 pm ]
Post subject: 

You have to declare another variable to store the user's input.
You have also stored the text into the array incorrectly.

Here is how it is done correctly:
var song : array 1 .. 6 of string := init ("sonne", "zwitter", "bodies", "buckdich", "ichwill", "duhast")


You also need to compare the user's input


var userinput:string
...
...
...
get userinput

if userinput = "sonne" then
Music.PlayFileLoop ("music/sonne.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
elsif userinput ="zwitter" then
..
...
...
..
end if

Author:  santabruzer [ Wed Feb 11, 2004 3:29 pm ]
Post subject: 

... dude.. why did you do that.. when you could've done this:
code:

setscreen ("graphics")

var song
var songs : array 1 .. 6 of string := init (sonne, zwitter, bodies, buckdich, ichwill, duhast)

for i : 1 .. 6
    put songs (i)
end for
put ""
put "Enter the song you wish to play:"

get song

for i : 1 .. 6
    if song = songs (i) then
        Music.PlayFileLoop ("music/" + songs (i) + ".mp3")
        Draw.Text (songs (i), 0, 0, defFontID, black)
    end if
end for


Author:  netninja [ Wed Feb 11, 2004 3:44 pm ]
Post subject: 

Laughing you ask why i did that??!!?? Because thats my knowledge of turing lol

But thanks alot, ill remember what you said! Thanks,

One more thing:

at the var songs: error says "syntax error at var, expected ':' "

and at the bottom at if song = songs (i) then: error says " operands of comparison operators must be scalars, sets, or strings.

Why is that?

Author:  recneps [ Wed Feb 11, 2004 3:53 pm ]
Post subject: 

umm... lol maybe because song has no type assigned to it Smile

code:
var song
thats all it says, its gotta be var song:string or int depending on how its used.

Author:  netninja [ Wed Feb 11, 2004 5:23 pm ]
Post subject: 

wow, that was shameful of me, so sorry,

lol, i cant believe i missed that, i guess my brain just skipped that step because its so basic lol, thanks for pointing it out though.

But why does it say that sonne is not declared?

Author:  Cervantes [ Wed Feb 11, 2004 5:36 pm ]
Post subject: 

sonne is just the text contained within the variable songs (1) . to call it you have to call it like so songs (1) .

Author:  netninja [ Wed Feb 11, 2004 6:01 pm ]
Post subject: 

no, the way santabruzer did it was by making (i) a song, i just cant seem to find the error in the code. I wanted to use that one because it seems more efficient, instead of having to declare 'if' so many times.

setscreen ("graphics")

var song :string
var songs : array 1 .. 6 of string := init (sonne, zwitter, bodies, buckdich, ichwill, duhast)

for i : 1 .. 6
if song = songs (i) then
put songs (i)
end for

put ""
put "Enter the song you wish to play:"

get song

for i : 1 .. 6
if song = songs (i) then
Music.PlayFileLoop ("music/" + songs (i) + ".mp3")
Draw.Text (songs (i), 0, 0, defFontID, black)
end if
end for

I just realized that there is two for statements, and two if statements, one of which has no end if. SO i ended the if, but the at the top it still gives the error i said before.

Author:  Paul [ Wed Feb 11, 2004 6:09 pm ]
Post subject: 

uh.. first of all the array is string, you have to put quotations in around the words, I don't see them...
code:

var songs : array 1 .. 6 of string := init (sonne, zwitter, bodies, buckdich, ichwill, duhast)

code:

for i : 1 .. 6
if song = songs (i) then
put songs (i)
end for %<---- forgot end if

the below prob is weird, i've always used more than 1 for loop with the same name and it worked, but if you change the below i to something else, that error won't occur...
code:

for i : 1 .. 6 %<--- thats weird, I've never had a prob with ths.
if song = songs (i) then
Music.PlayFileLoop ("music/" + songs (i) + ".mp3")
Draw.Text (songs (i), 0, 0, defFontID, black)
end if
end for


oh yea, the variable song has no value, so you can't use this:

code:

if song = songs (i) then
 

without first assigning song a value.

Author:  netninja [ Wed Feb 11, 2004 6:19 pm ]
Post subject: 

Is song's value not song = songs + i ???
When i take away the 'then' , it gives me another error.

Also, now that i have the quotes, it says too few initial values, but thats not tru

Author:  Paul [ Wed Feb 11, 2004 6:22 pm ]
Post subject: 

well there are problems here, what do you want to do?
if you need to compare 2 values, both variables MUST have values before you compare them.

like if you compare song and songs(i) to see if they're equal, song must first have a value to be compared.

If your trying to make song=songs(i) then the if statement is no good.
You have to have song:=songs(i)

Author:  netninja [ Wed Feb 11, 2004 6:29 pm ]
Post subject: 

I want to be able to play the song which the user typed in, and thats it lol.

Mr Santabruzer gave me that code, and so far its been a drag Crying or Very sad

Author:  Paul [ Wed Feb 11, 2004 6:33 pm ]
Post subject: 

he's code is fine, I just want to know why you have this:
for i : 1 .. 6
if song = songs (i) then
put songs (i)
end for
before you assigned a value to song.
I didn't see it in his code.

Author:  santabruzer [ Wed Feb 11, 2004 6:33 pm ]
Post subject: 

ignore the Mr. Santabruzer code.. and do it the regular way.. once you are done that.. Mr. Santabruzer can implement his code upon your program.. if need be.. for effienciency.. Razz

Author:  netninja [ Wed Feb 11, 2004 6:35 pm ]
Post subject: 

Ok, i did it, but somethings wrong with this one, and i know what it is lol
But i cannot fix it hehehe..

setscreen ("graphics")

var sonne, zwitter, bodies, buckdich, ichwill, duhast:string
var userinput:string

put "sonne"
put "zwitter"
put "bodies"
put "buckdich"
put "ichwill"
put "duhast"
put ""
put "Enter the song you wish to play:"

get userinput
sonne:=userinput
zwitter:=userinput
bodies:=userinput
buckdich:=userinput
duhast:=userinput
ichwill:=userinput



if userinput = sonne then
Music.PlayFileLoop ("music/sonne.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= zwitter then
Music.PlayFileLoop ("music/zwitter.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= bodies then
Music.PlayFileLoop ("music/bodies.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= buckdich then
Music.PlayFileLoop ("music/buckdich.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= ichwill then
Music.PlayFileLoop ("music/ichwill.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput = duhast then
Music.PlayFileLoop ("music/duhast.mp3")
Draw.Text ("Du Hast",0,0, defFontID, black)
end if

Author:  netninja [ Wed Feb 11, 2004 6:37 pm ]
Post subject: 

I also have another way, that is better i think, one sec, im going to have to write it.

Author:  santabruzer [ Wed Feb 11, 2004 6:38 pm ]
Post subject: 

literal string Rolling Eyes
code:
setscreen ("graphics")

var userinput:string

put "sonne"
put "zwitter"
put "bodies"
put "buckdich"
put "ichwill"
put "duhast"
put ""
put "Enter the song you wish to play:"

get userinput

if userinput = "sonne" then
Music.PlayFileLoop ("music/sonne.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= "zwitter" then
Music.PlayFileLoop ("music/zwitter.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= "bodies" then
Music.PlayFileLoop ("music/bodies.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= "buckdich" then
Music.PlayFileLoop ("music/buckdich.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput= "ichwill" then
Music.PlayFileLoop ("music/ichwill.mp3")
Draw.Text ("Sonne", 0, 0, defFontID, black)
end if

if userinput = "duhast" then
Music.PlayFileLoop ("music/duhast.mp3")
Draw.Text ("Du Hast",0,0, defFontID, black)
end if

Author:  Paul [ Wed Feb 11, 2004 6:38 pm ]
Post subject: 

I haven't had anything go wrong, except the text overlaps? is that what you mean? oh yea, and is duhast Ramstein?
I also don't get why this is here:
code:

if userinput = sonne then

when you have this before it
code:

sonne:=userinput

theres no point to that if statement, because whatever the user inputs, the user input will be always equal to all of them, so the text overlaps?

Author:  santabruzer [ Wed Feb 11, 2004 6:39 pm ]
Post subject: 

yea.. i think it is.. well at least the last time i checked.. Rolling Eyes

Author:  netninja [ Wed Feb 11, 2004 6:41 pm ]
Post subject: 

All those are Rammstein except for bodies (Let the bodies hit the floor) by i forgot lol

I just didnt want to write out the whole thing. It seems that rock music is really good when your playing a game, it livens you up. Or at least makes you feel like playing. I think it works best on killing games. I donno what im talking about, but thanks for the help.

Thats exactly what i was about to do, but i didnt finish.

But i wanted to learn how to do it using (i)
Could you help please? Sorry, im eating, very slow. Sorry to be such a botherer, if you could point me in the right direction would be the best to teach me how i could do it using the (i). Im having troubles with it.

Author:  santabruzer [ Wed Feb 11, 2004 6:50 pm ]
Post subject: 

and array i simple one "var" with alot of different values.. think of it.. as many Variables compressed into one.. so if you declare and array with

code:
var test : array 1 .. 5 of string


there are going to be 5 values to test.. hence test (1), test (2) ... test (5).

now.. if for example i want to give them all a zero value.. i could either initialize them.. or i can do the following:

code:
for i : 1 .. 5
test (i) := 0
end for


there basically all i'm doing is for every value of the array i'm making the value equal to zero.. by accessing the array with the brackets.. after the variable..

In your problem.. it's easier to use an array, because you are doing the same thing over and over again... so you could do something like this:

code:
var songs : array 1 .. 5 := init ("asdasdasd", "aaasadsd","abdasdasd","avdasasdd","aasdasasdd")
for i : 1 .. 5
     put songs (i)
end for
% The above will output the songs name

var choice : string
% Have a variable for the user's song choice

for i : 1 .. 5
if choice = songs (i) then
     % Play the song here.. etc.. Output the name by putting "songs (i)", no quotes..

end for
% This will compare the user's input to the array.

Author:  netninja [ Wed Feb 11, 2004 6:55 pm ]
Post subject: 

Thanks

Author:  netninja [ Wed Feb 11, 2004 7:01 pm ]
Post subject: 

Why does it say "too few initial values?" thats exactly what it said before...

var songs : array 1 .. 6 of string:= init ("sonne, duhast, ichwill, bodies, buckdich, zwitter")

for i : 1 .. 5

put songs (i)
end for

var choice : string
for i : 1 .. 5

if choice = songs (i) then
Music.PlayFileLoop ("music/"+songs (i)+".mp3")
Draw.Text (songs (i), 0, 0, defFontID, black)

end if end for

i understood almost everything you said before, but when you explained it, i thought i understood everything 100%. and still gets the same error, im Mad

Author:  santabruzer [ Wed Feb 11, 2004 7:11 pm ]
Post subject: 

because.. you must put quotes as follows:
code:
var songs : array 1 .. 6 of string := init ("sonne", "duhast", "ichwill", "bodies", "buckdich", "zwitter")

Author:  netninja [ Wed Feb 11, 2004 7:14 pm ]
Post subject: 

Ahh, thats important heh, thanks alot, want some bits? Although i dont know how to give them to you heh

That helped a LOT! Whew. NOoooOOOOOooo, more problems!

It says choice has not been declared, where do i put get choice? NVM BOOYAH, i got it, finally, array has been mastered! I just didnt check hard enough. Now onto records... Array is still not fully mastered Wink

Author:  Paul [ Wed Feb 11, 2004 7:17 pm ]
Post subject: 

You donate by clicking donate beneath his pic, (see? I help). I'll donate bits to him for you, seeing as you don't have much.

Author:  netninja [ Wed Feb 11, 2004 7:22 pm ]
Post subject: 

Thanks alot, for teaching me, and for donating.

Author:  santabruzer [ Wed Feb 11, 2004 8:23 pm ]
Post subject: 

I have a feeling i have to do this anyways.. but i'll do my txt program.. as an example.. ok..

what is a record?

A record is a very simple way to record information. Hence the name. Like a company record, what a record does, is give many variables.. such as name, company, s/n.. etc.. all assigned under one varaible.. and thus makes life so much easier..
Records could also be used for x and y locations..

code:
type coord : record
      x, y : int
end record


that's is how i declared the Co-Ord record.. now i must declare the "universal" variable, for these records.. and so i get:
code:
var text : coord

This would mean that text has to "sub" variables. as declared in the record before.. so it would be:

code:
text.x
text.y


now.. obviously this can be used with an array.. for some things.. like if you want to create some records.. for iono.. a company you would do:

code:
type companyfile : record
name, address : string
sn : int
end record


.. hence you would have one record.. then declare a variable .. and you'd probably want to declare an array.. becuase there are probably gonna be more than one person right?

code:
var employee : array 1 .. 5


then you could do the following:

code:

for i : 1 .. 5
    put "Please enter your name:"
    get employee (i).name : *
    put "Please enter your address:"
    get employee (i).address : *
    put "Please enter your S/N:"
    get employee (i).sn
end for


there you go... if you actually want to see a cool program.. reply with : "i want to see a cool program".. of course that is if you didn't see my mouse name display program Razz

Author:  netninja [ Wed Feb 11, 2004 9:23 pm ]
Post subject: 

i want to see a cool program Razz

Author:  santabruzer [ Wed Feb 11, 2004 10:09 pm ]
Post subject: 

ha ha.. found it Very Happy
http://www.compsci.ca/v2/viewtopic.php?t=3003


: