Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Boolean and if statement help
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sammi




PostPosted: Sat Oct 15, 2011 4:56 pm   Post subject: Boolean and if statement help

What is it you are trying to achieve?
<Im making a magic 8 ball for a project and im trying make sure that people cant just put in a bunch of numbers (i.e "405968245") and get an answer>


What is the problem you are having?
<there is an error message popping up every time i hit run and it says my "if statement" must be boolean and im not even sure if the code in my if statement is right.>


Describe what you have tried to solve this problem



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<var Question : string
var words : array 1 .. 11 of string

% parts of the array
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"

locatexy (200,390)% where "ask a question" turns up on the screen

Draw.FillOval(330,250,100,100, brightred)% drawing the ball

var Answers : int

put "Ask the Magic 8-Ball a question"
locatexy (250,250)% where the typing cursor is

loop

% make the person put in words, not numbers
if Question := int then
put "thats not a question"
elsif
get Question : *
locatexy (200,100)% where the answer will show up on the screen



% put any one of my answers on the screen
Answers := Rand.Int (1,11)
put words (Answers)
locatexy (320,250)% reset the cursor

end loop
delay(20)

>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Oct 15, 2011 5:24 pm   Post subject: Re: Boolean and if statement help

sammi @ Sat Oct 15, 2011 4:56 pm wrote:
im not even sure if the code in my if statement is right.

It's not.
code:

if Question := int then

That's... not how things work. You are trying to assign ( := operator) a type int as a value to variable Question. Even if you could somehow compare types, this would never make sense, since
code:

var Question : string

Question is always a string. "1234" (in quotes) is still a string.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sammi




PostPosted: Sat Oct 15, 2011 6:04 pm   Post subject: RE:Boolean and if statement help

then how do i fix it?
Tony




PostPosted: Sat Oct 15, 2011 6:10 pm   Post subject: RE:Boolean and if statement help

well, what exactly do you want to check for here?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Aange10




PostPosted: Sat Oct 15, 2011 6:28 pm   Post subject: Re: Boolean and if statement help

Your coding isn't correct. But I can help you with your project. First and Foremost, you need to read the Turing Walkthrough. It will give you a nice understand of Turing. Secondly, you're looking at string manipulation (scary, i know. But it's easy(: ) to solve your problem. That can be found under the Turing Walkthrough, or directly from this link: http://compsci.ca/v3/viewtopic.php?t=8229.

Now, on to your project.

sammi wrote:

<there is an error message popping up every time i hit run and it says my "if statement" must be boolean and im not even sure if the code in my if statement is right.>


Turing is giving you an error because the if statement must come out with one of two answers. True or False. So the problem lies within your statement. As tony said, ':=' means define.
The other problem with your if statement is that Question is already defined as a string.
code:

    var Question : string
 

So there is no way Question is an integer, if it's a string. That's why that doesn't work. However, there is a way Question can contain an integer. Finding how how to do that can be found in the second link I gave you; String Manipulation.

However, you're in luck as I'm willing to give you a hand with your coding Smile. So lets start, shall we?
First and foremost we need to define our variables.
Turing:

var Question : string
var Answer : int
var valid : boolean := true
var words : array 1 .. 11 of string
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"

Pretty straight forward. Incase your wondering, var valid : boolean := true is a quick way of saying
code:

var valid : boolean
valid := true

(A boolean variable holds one of two values; True or False.)

Now that we have our variables, we'll want to get our nice 8 ball out there, and get some magic questions going!
Turing:

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)
% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"
% Getting a response
locatexy (250, 250)
get Question : *


So this is pretty straight forward. We drew the ball, output a phrase to the screen, and we asked for a response. Now comes the tricky part, String Manipulation.

Turing:

% Make sure the question contains no numbers
for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for


I know what your thinking... Yeah, that's a lot of new things. Lets dissect it piece by piece. Hopefully you know fors, if not I recommend you read through the Turing Walkthrough a bit.

code:

for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.


If you don't know what a for is, all it is is a loop that only loops a number of times [opposed to indefinitely]. Here, the for i starts at 1 and runs through the entire length of the string Question. And for b runs through 1-9. The important thing, now, is our if statement.

code:

if Question (i) = intstr(b) then


This here is saying if the string Question position number (i) is = to (b) then. The instr is there just so that we can change b to a string. So that it may look through Question (also a string!).
Now the next part is to make sure that if that statement becomes true (i.e we found a number in our string) that it does something.
Turing:

if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for


Now this here says that if the statement is true, then it sets our valid variable to false. If you look after that, it says that if valid is not true, then it gives them the error message and exits the for.
If they did follow the rules, and no integer was inputed, then it continues past the for, to our last piece of code.

Turing:

if valid then
    locatexy (200, 100)
    Answer := Rand.Int (1, 11)
    put words (Answer)
end if


Our last bit here says that if the question is valid then it randomly selects an answer, and puts it on the screen.
So here is the code all together.
Turing:

var Question : string
var Answer : int
var valid : boolean := true
var words : array 1 .. 11 of string
words (1) := "yes"
words (2) := "no"
words (3) := "maybe"
words (4) := "thats for me to know and you to find out Smile"
words (5) := "of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absulutely"
%% Program
% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)
% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"
% Getting a response
locatexy (250, 250)
get Question : *
% Make sure the question contains no numbers
for i : 1 .. length (Question) % This for is Representing the Character of the string
    for b : 1 .. 9 % This for checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false
            exit
        end if
    end for
    if valid then
    else
        cls
        put "Numbers found! Please do not use numbers."
        delay (2000)
        exit
    end if
end for
if valid then
    locatexy (200, 100)
    Answer := Rand.Int (1, 11)
    put words (Answer)
end if


*suggestion* You should add a way to ask the user if they'd like to repeat (:
sammi




PostPosted: Sat Oct 15, 2011 6:31 pm   Post subject: RE:Boolean and if statement help

nvr mnd scrap the numbers thing.

how can i assign certain answers to come up when a certain kind of question is asked like:
Q: will i ever become rich?
A: I doubt it
sammi




PostPosted: Sat Oct 15, 2011 6:35 pm   Post subject: RE:Boolean and if statement help

omg thank you so much!!!!!!
sorry i posted that last reply " nvr mnd scrap the numbers thing" without refreshing the page and i didnt see ur post srry bout that.

thanks again!!!!!
Aange10




PostPosted: Sat Oct 15, 2011 6:36 pm   Post subject: Re: Boolean and if statement help

sammi wrote:

nvr mnd scrap the numbers thing.

how can i assign certain answers to come up when a certain kind of question is asked like
Q will i ever become rich?
A I doubt it


Well first off, reading the Turing Walkthrough would teach you everything you need to know. But to answer your question... Tell me, what do you want to happen?


EDIT: No problem(:
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sat Oct 15, 2011 6:41 pm   Post subject: RE:Boolean and if statement help

You could see is a question is an exact match to something that you have thought of, but you are unlikely to hit those. E.g.

"Will I become <adjective> rich in the next <N> <time-units>?"

http://en.wikipedia.org/wiki/Natural_language_processing is hard. It is really really really hard.

But you can approximate this with checking if a particular word(s) are present in the question. It'd be more interesting if you gave a custom answer too, but that's up to you.

Q: anything that includes word "rich"
A: I'm not a certified financial advisor.

index is the function that you'll be looking for.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
sammi




PostPosted: Sat Oct 15, 2011 6:45 pm   Post subject: RE:Boolean and if statement help

haha im not a certified financial advisor i pretty clever.
i'll take a look at the links but BTW i may be back ......
thanks again,
sammi
sammi




PostPosted: Sat Oct 15, 2011 6:46 pm   Post subject: RE:Boolean and if statement help

is pretty clever*
sorry lol
Aange10




PostPosted: Sat Oct 15, 2011 6:52 pm   Post subject: RE:Boolean and if statement help

Just edit your posts, Sammi, instead of making new ones.
sammi




PostPosted: Sat Oct 15, 2011 6:53 pm   Post subject: RE:Boolean and if statement help

i didnt even know i could edit them.... oops
im new to this site
sammi




PostPosted: Sat Oct 15, 2011 6:57 pm   Post subject: RE:Boolean and if statement help

alright im already back lol

i understand the index thing but i dont know how to intergrate it into my existing code
Tony




PostPosted: Sat Oct 15, 2011 6:59 pm   Post subject: RE:Boolean and if statement help

if-statements
loops
reading-from-a-file (will make your life so much easier)
arrays (optional. reading from a file every time is slow)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 45 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: