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

Username:   Password: 
 RegisterRegister   
 If command help!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cheburashka




PostPosted: Tue Apr 21, 2009 7:22 pm   Post subject: If command help!

I am supposed to let the computer select one number between 50 and 100. The user gets three guesses to get it right. If they guess correctly before the third guess the program exits he loop and displays a message

this is what i have so far however it doesnt work
%Declaration Section
var guess, guess2, guess3, answer : int
var tries : int := 0

%Program Title
procedure title (programTitle : string)
cls
locate (1, 33)
put "Guessing Game"
put " "
end title
proc pauseProgram
var reply : string (1)
put " "
put "Press any key to continue..."
getch (reply)
end pauseProgram
%Program introduction
procedure introduction
title (" ")
locate (3, 1)
put "See if you can guess a number between fifty and one hundred!"
end introduction
%Random number
proc randNum
title (" ")
randint (answer, 50, 100)
put "The number from 50 to 100 is: ", answer
end randNum
%User Input
procedure userInput
var reply : string (1)
locate (5, 1)
put "Enter a number between 50 and 100: " ..
get guess
tries := tries + 1
for x : 1 .. 100
if guess > 100 then
put "Error the number should be between 50 and 100, enter again: "
get guess
elsif guess < 50 then
put "Error the number should be between 50 and 100, enter again: "
get guess
end if
end for
if guess < answer then
put "Too low, try again: " ..
get guess2
elsif guess > answer then
put "Way up there? try again"
get guess2
else
put "Congratulations you guessed the number!"
end if

end userInput
%Processing&Output
proc display %Possible guess results-you must add the if structure!


end display
proc goodBye
locate (20, 20)
put "Program by:Sabina Rafikova"
end goodBye
%Main Program
pauseProgram
randNum
introduction
loop
userInput
display
exit when tries = 3 or guess = answer
end loop
goodBye
%End Program


My question is how do i make the 3 tries without using the if command over and over, i know that I am supposed to use var tries, and then put tries:=tries=1, however I dont get how it works
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Apr 21, 2009 8:02 pm   Post subject: RE:If command help!

use proper variable names. In
Turing:

for x : 1 .. 100
if guess > 100 then
put "Error the number should be between 50 and 100, enter again: "
get guess
elsif guess < 50 then
put "Error the number should be between 50 and 100, enter again: "
get guess
end if
end for

what does x stand for?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dusk Eagle




PostPosted: Tue Apr 21, 2009 9:37 pm   Post subject: Re: If command help!

Tony, I think you're being a little harsh in your example. It is generally acceptable to name your "for loop variables" as a single letter of the alphabet; teachers even teach it this way. This is commonly done with i rather than x, but does it really matter?

Cheburashka, highlight your code by sticking it in between a [syntax="turing"] and a [/syntax]. Now, onto your program. What is the for loop supposed to do in this chunk of code anyway?
Turing:

    for x : 1 .. 100
        if guess > 100 then
            put "Error the number should be between 50 and 100, enter again: "
            get guess
        elsif guess < 50 then
            put "Error the number should be between 50 and 100, enter again: "
            get guess
        end if
    end for

There is no need to pester the user 100 times about this issue. In fact, the only reason the user doesn't get pestered 100 times seems to lie in a quirk with Turing, where global variables can not be "get"ted more than once in a procedure without being skipped over.

Speaking of global variables, you should really try to avoid using them whenever possible. One thing I think will help you cut down on the number of global variables is something called a function, which is almost exactly like a procedure, so there's no need to worry. Go read about them here.
Tony




PostPosted: Tue Apr 21, 2009 9:45 pm   Post subject: Re: If command help!

I was going to lead towards the
Dusk Eagle @ Tue Apr 21, 2009 9:37 pm wrote:
What is the for loop supposed to do...?

question.

It seems that you are pointing at the same snippet of code anyway.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Cheburashka




PostPosted: Tue Apr 21, 2009 9:47 pm   Post subject: Re: If command help!

Tony @ Tue Apr 21, 2009 9:45 pm wrote:
I was going to lead towards the
Dusk Eagle @ Tue Apr 21, 2009 9:37 pm wrote:
What is the for loop supposed to do...?

question.

It seems that you are pointing at the same snippet of code anyway.

The for loop makes the error repeat so that if the user makes an error twse he will get the error message
if i dont use the for loop it only shows the error once
Dusk Eagle




PostPosted: Tue Apr 21, 2009 9:53 pm   Post subject: Re: If command help!

Replace that for loop with the one below and see what happens.
Turing:

for x : 1 .. 100
        if guess > 100 then
            put "Error the number should be between 50 and 100, enter again: "
            get guess
        elsif guess < 50 then
            put "Error the number should be between 50 and 100, enter again: "
            get guess
        end if
        break
    end for

Notice how it keeps hitting the break statement without waiting for the user input (assuming guess < 50 or >100)? This is clearly not what you want. Besides, why only continue 100 times? Why not continue forever until a condition is met?

BTW, I was wrong about that quirk in Turing. I realize what the problem was now.
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 1  [ 6 Posts ]
Jump to:   


Style:  
Search: