Computer Science Canada

make a list to call in an if condition

Author:  Computer [ Tue Jun 24, 2003 7:21 am ]
Post subject:  make a list to call in an if condition

I want to start making an interactive program that a user can take to. so can i do something like this so when a user greets the computer it can say many different things. so create a group of things a user could say.

list := hello, hi, bonjour, yo, sup ...etc

if user = list then
put " Hi there"
else
put " your not very polite didn't even say hi"
end if

i know this isn't real code but its just to illustrate my question.
How would I go about actually coding this?

Author:  Mazer [ Tue Jun 24, 2003 9:09 am ]
Post subject: 

i could see 1 or 2 ways of doing what your asking. the first would be to use an array.

code:

var list : array 1 .. 5 of string := init ("hello", "hi", "bonjour", "yo", "sup")


and then, once you get the input from the user check each

code:

for i : 1 .. 5
    if user = list (i) then
        % stuff happens here
    end if
end for


but that wouldn't be so great.

the other idea would be using index. it's a function that results the starting position of one string inside another string (results 0 if it wasn't found).

code:

var list : string := "hello hi bonjour yo sup"


then get the user input and check to see if what they typed was inside the list:

code:

if index (list, user) > 0 then % they inputed one of the greetings
    put "hello to you"
end if


of course, it would matter that the user spells the word correctly and it has to be case sensitive (ie, "hello", not "Hello")

Author:  Andy [ Tue Jun 24, 2003 10:30 am ]
Post subject: 

lol mazer, i dun think he understands arrays yet, hey computer go to the tutorial section and click on the arrays tutorial, by me, not by tony, actually here's the link
http://www.compsci.ca/bbs/viewtopic.php?t=1117

but first, learn about strings first

Author:  Computer [ Tue Jun 24, 2003 4:48 pm ]
Post subject: 

I do know about strings, but hasch is a bit confusing. But you're right i dont know anything about arrays.

Author:  naoki [ Tue Jun 24, 2003 10:36 pm ]
Post subject: 

hasch checks if any keyboard button has been pressed
i think it's better than just getch because the program can keep running instead of waiting for input

Author:  Blade [ Wed Jun 25, 2003 11:06 am ]
Post subject: 

Input.KeyDown rules them both... instead of using the array algorythim, use the index, more efficient

Author:  Andy [ Wed Jun 25, 2003 11:33 am ]
Post subject: 

i'd have to disagree with you blade, it all depends on the situation. if ur making a game that runs very fast, Input.KeyDown does work much better, but if ur program runs very slowly hasch and getch will work better since you'd want to use the buffer then again of course, you could enhance both in ur program

Author:  PaddyLong [ Wed Jun 25, 2003 12:26 pm ]
Post subject: 

hasch is great if you even just want to wait for them to push any button and situations like that

Author:  Andy [ Wed Jun 25, 2003 1:44 pm ]
Post subject: 

ya Input.KeyDown takes too long to set up when campared to hasch and getch of course


: