Computer Science Canada

Play again help

Author:  belarussian [ Sun Jan 03, 2010 2:18 pm ]
Post subject:  Play again help

ok so i need help asap i did this before but i forgot how to do it again so basiclly how do you do a index for a playagin thing for example:

playagain := getchar
playagain := index ('YyNn', playagain) > 0

if playagain = 'N' or playagain = 'n' then
exit
end if

Author:  TheGuardian001 [ Sun Jan 03, 2010 2:57 pm ]
Post subject:  Re: Play again help

take out the line with index (that's not how index works) and put the rest inside a loop.

Author:  belarussian [ Sun Jan 03, 2010 4:08 pm ]
Post subject:  RE:Play again help

no i need it to work with index so that there will be no other keys pressed that will have an affect

Author:  imbored [ Sun Jan 03, 2010 6:22 pm ]
Post subject:  RE:Play again help

you dont need index do you o.o''. just loop the getch untill you get either a Y,y, or N,n and then let it end the loop then finally to the if statements

Author:  belarussian [ Sun Jan 03, 2010 8:13 pm ]
Post subject:  RE:Play again help

ok look it doesn't work like that i need the damn index if anyone who is going to write dont use index plz save your breaath cuz i wont listen to you

Author:  TheGuardian001 [ Sun Jan 03, 2010 8:43 pm ]
Post subject:  Re: Play again help

belarussian wrote:

ok look it doesn't work like that

Yes it does.
Why do you need index? the string has a length of 1, since you assigned it with getchar... You might as well just check an if statement.

code:

var x : string
loop
    put "would you like to continue?(Y/N)"
    x := getchar %String is now length 1! using index on it would be pointless and stupid...
    if x = 'N' or x = 'n' then
        exit
    end if
end loop
put "Program finished (without index)"


HOWEVER
If you absolutely insist on using index (You shouldn't, it's stupid), you could try not assigning a boolean value to a string (which is only slightly more stupid than using index in the first place).

index ("YyNn", playagain) > 0 is a boolean value. So when you assign it to playagain (which is a string), it fails. exit when index("YyNn",playagain) > 0 would be a slightly more correct solution, however it would also fail, as you're looking through a string that has both Y and N in it.
So if the user enters "Y" or "y" (to continue), index will return > 0, and the loop will exit. On the other hand, if they enter "N" or "n", index will still return > 0, and the loop will exit.

You should be comparing against only one of "Yy" or "Nn", not both at the same time, since you aren't using an if statement, you won't be able to tell whether they pressed y or n if you check against "YyNn".

Ultimately,
code:

exit when index ("Nn",playagain) > 0

code:

exit when playagain = "N" or playagain = "n"

both do the same thing, however index requires more time and resources to do the same amount of work. It's like walking over a hill when there's a perfectly good tunnel straight through.


: