Computer Science Canada

program 'end loop's before I tell it to

Author:  Insectoid [ Wed Mar 26, 2008 4:17 pm ]
Post subject:  program 'end loop's before I tell it to

This program is a learning project for me which uses some commands I have figured out for myself which weren't taught in my class yet (gr. 10 computer programming)
All problems are explained in the code , beginning in 'ERROR'

code:

% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
loop

    put "This program will show you (inaccuratly)"
    put "How long you have left to live."

    % optional start
    put "would you like to know? (yes or no)"
    get seeage
    exit when seeage = "no"


    %input (age)

    put "Enter your age by typing on the keyboard."
    get age

    %calculations
    total := 81 - age

    % a message to the user based on age
    %ERROR only some work.
    put "you have ", total, " years left to live"
    if total < 0 then
        put "your dead!"
    elsif total > 0 and total < 20 then
        put "you haven't got much time left!"
    elsif total > 20 and total < 30 then
        put "getting on in years"
    elsif total > 30 and total < 40 then
        put "midlife crisis, anyone?"
    elsif total > 40 and total < 50 then
        put "Time to settle down"
    elsif total > 50 and total < 60 then
        put "your rebellious years are over. get used to it."
    elsif total > 60 and total < 0 then
        put "why do you care? you've got lots of time left!"
    elsif total = 0 then
        put "TIme to organize a funeral!"

       
        %restart the program.
        % ERROR Doesn't seem to work, always reverts to the beginning
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        exit when seeage = "no"
    end if

end loop

Author:  Mackie [ Wed Mar 26, 2008 4:23 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Ok, all you really need to do is move the 'end if' from after the:

code:
exit when seeage = "no


to before the comment '%restart the program.'

In it's current state you only get the decision if you were 81.

Author:  Insectoid [ Wed Mar 26, 2008 4:56 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Ah! Thank you! That solved everything! We actually haven't done looping yet in class, it was something I figured out for myself, so I don't know everything yet. I don't even know what 'end if' does, it's only there because turing told me to put it there.

My teacher's a dink, actually, he's spent 3 days teaching us what names you can't give to a variable/constant/etc.

Thank's for a quick reply!

Author:  Sean [ Wed Mar 26, 2008 4:59 pm ]
Post subject:  Re: program 'end loop's before I tell it to

The end if is to end the if statement that you were using.

And any loop questions, or any other general ones, all of us are here to help.

Author:  Insectoid [ Wed Mar 26, 2008 5:12 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Just adjusted it a bit, to show what I learn on my own!

added some loops so that it only shows the first 'would you like to play' bit once, then skips every subsequent loop. 10 minutes ago, I had no clue how to do this!

code:

% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
loop
    loop
        put "This program will show you (inaccuratly)"
        put "How long you have left to live."

        % optional start
        put "would you like to know? (yes or no)"
        get seeage
        cls
        exit when seeage = "yes"
    end loop

    %input (age)
    loop
        put "Enter your age by typing on the keyboard."
        get age

        %calculations
        total := 81 - age

        % a message to the user based on age
        %ERROR only some work.
        put "you have ", total, " years left to live"
        if total < 0 then
            put "your dead!"
        elsif total > 0 and total < 20 then
            put "you haven't got much time left!"
        elsif total > 20 and total < 30 then
            put "getting on in years"
        elsif total > 30 and total < 40 then
            put "midlife crisis, anyone?"
        elsif total > 40 and total < 50 then
            put "Time to settle down"
        elsif total > 50 and total < 60 then
            put "your rebellious years are over. get used to it."
        elsif total > 60 and total < 0 then
            put "why do you care? you've got lots of time left!"
        elsif total = 0 then
            put "TIme to organize a funeral!"

        end if
        %restart the program.
        % ERROR Doesn't seem to work, always reverts to the beginning
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        exit when seeage = "no"


    end loop
end loop

also, the F2 tabbing thing I found out on my own too, after seeing my teacher do it (but I didn't see what button(s) he pressed)

Author:  Sean [ Wed Mar 26, 2008 5:18 pm ]
Post subject:  Re: program 'end loop's before I tell it to

You don't need the extra loops.

It was fine the way you had it before.

Turing:

%variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
loop

    put "This program will show you (inaccuratly)"
    put "How long you have left to live."

    % optional start
    put "would you like to know? (yes or no)"
    get seeage
    exit when seeage = "no"


    %input (age)

    put "Enter your age by typing on the keyboard."
    get age

    %calculations
    total := 81 - age

    % a message to the user based on age
    %ERROR only some work.
    put "you have ", total, " years left to live"
    if total < 0 then
        put "your dead!"
    elsif total > 0 and total < 20 then
        put "you haven't got much time left!"
    elsif total > 20 and total < 30 then
        put "getting on in years"
    elsif total > 30 and total < 40 then
        put "midlife crisis, anyone?"
    elsif total > 40 and total < 50 then
        put "Time to settle down"
    elsif total > 50 and total < 60 then
        put "your rebellious years are over. get used to it."
    elsif total > 60 and total < 0 then
        put "why do you care? you've got lots of time left!"
    elsif total = 0 then
        put "Time to organize a funeral!"
    end if

        %Restarts program
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        exit when seeage = "no"
    end if

end loop

Author:  Insectoid [ Wed Mar 26, 2008 5:24 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

But that way, after you type 'yes' in the 'restarts program' section (or any other word that isn't 'no') you then had to type it in again in the 'introduction to program' section.

ex. 'would you like to play again?'
type: yes
then : 'would you like to play?'

its like playing a game that goes through all the opening credits when you go to the menu *couph*bowman 2*couph*

Author:  Sean [ Wed Mar 26, 2008 5:27 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Do it your way then, I was just removing the extra loops because I felt they weren't needed. If someone accidently continued your program when they wanted to exit, it then allows them to have the second opportunity.

Author:  Insectoid [ Wed Mar 26, 2008 5:37 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

I se your reasoning, but that is what the little X in the corner of the dialog box is for.

This is starting to sound a bit...heated. Don't know why, but it is.

Author:  Sean [ Wed Mar 26, 2008 5:43 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Naw, it wasn't getting heated. If I made it look like it then I am sorry.

It's up to you on the way you want to do it.

Also, I suggest adding some cls 's into your program to clear away any unnecessary data that is showing after you reset.

Author:  Insectoid [ Wed Mar 26, 2008 5:47 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Darn you, you posted while I was editing the thing!
anyway, I wasn't bothered, it looked to me like you were getting a bit defensive, maybe thought I was being..you know what, never mind.

Anyway, I was going to say (until you rudefully replied to my post while I was editing it) that this is my 'learning program' that will see many frequent updates as I learn more about turing, like now I am about to change the background color, which I just learned how to do.

Author:  Sean [ Wed Mar 26, 2008 5:48 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Use a cls after the colourback so it does the entire screen, not the given text areas that are present on your output.

Author:  Insectoid [ Wed Mar 26, 2008 5:55 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Ha! you solved my problem before it even came up! I just going to ask how to fix it!
seems it doesn't work if you use drawfillbox as I saw somebody do. Now I just have to wait for my teacher to tell me why you do it that way, because then I can figure more things out!

Author:  Sean [ Wed Mar 26, 2008 5:57 pm ]
Post subject:  Re: program 'end loop's before I tell it to

You can use Draw.FillBox but you have to do it before everything. It works in layers, your put statements would go first, if you had the box, then the box would be over top of what was before it. Doing it before, layers it at the bottom.

Author:  Mackie [ Wed Mar 26, 2008 5:57 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

The screen is only updated around where the text is . When you clear the screen it is wiped and the full color is shown.

Author:  Insectoid [ Wed Mar 26, 2008 6:00 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

what, and then the text is printed overtop?
don't bother, my teacher will (eventually) explain it.

Author:  Insectoid [ Wed Mar 26, 2008 6:04 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Crap, now it doesn't do the 'message to the user based on age' again. here's what I've got:

code:

% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
setscreen ("graphics: 500; 100")
colorback (yellow)
cls
loop

    loop
        color (blue)
        put "This program will show you (inaccuratly)"
        put "How long you have left to live."

        % optional start
        put "would you like to know? (yes or no)"
        get seeage
        cls
        exit when seeage = "yes"
    end loop

    %input (age)
    loop
        put "Enter your age by typing on the keyboard."
        get age
        cls
        %calculations
        total := 81 - age

        % a message to the user based on age
        %ERROR only some work.
        put "you have ", total, " years left to live"
        if total < 0 then
            put "your dead!"
        elsif total > 0 and total < 20 then
            put "you haven't got much time left!"
        elsif total > 20 and total < 30 then
            put "getting on in years"
        elsif total > 30 and total < 40 then
            put "midlife crisis, anyone?"
        elsif total > 40 and total < 50 then
            put "Time to settle down"
        elsif total > 50 and total < 60 then
            put "your rebellious years are over. get used to it."
        elsif total > 60 and total < 0 then
            put "why do you care? you've got lots of time left!"
        elsif total = 0 then
            put "TIme to organize a funeral!"

        end if
        %restart the program.
        % ERROR Doesn't seem to work, always reverts to the beginning
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        cls
        exit when seeage = "no"


    end loop
end loop

Author:  Sean [ Wed Mar 26, 2008 6:05 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Let me explain this a little more clearer.

Take your program..

Turing:

%variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
loop

    put "This program will show you (inaccuratly)"
    put "How long you have left to live."

    % optional start
    put "would you like to know? (yes or no)"
    get seeage
    exit when seeage = "no"


    %input (age)

    put "Enter your age by typing on the keyboard."
    get age

    %calculations
    total := 81 - age

    % a message to the user based on age
    %ERROR only some work.
    put "you have ", total, " years left to live"
    if total < 0 then
        put "your dead!"
    elsif total > 0 and total < 20 then
        put "you haven't got much time left!"
    elsif total > 20 and total < 30 then
        put "getting on in years"
    elsif total > 30 and total < 40 then
        put "midlife crisis, anyone?"
    elsif total > 40 and total < 50 then
        put "Time to settle down"
    elsif total > 50 and total < 60 then
        put "your rebellious years are over. get used to it."
    elsif total > 60 and total < 0 then
        put "why do you care? you've got lots of time left!"
    elsif total = 0 then
        put "Time to organize a funeral!"
    end if

        %Restarts program
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        exit when seeage = "no"
        cls %Clears the screen after you respond, if yes, then everything is erased.
    end if

end loop


The cls will erase all the output show when you place it somewhere. If you were to use a Draw.FillBox for the background, you would place it at the very beginning of the program. What you would want is...

Turing:


The reason to put it at the beginning is because of layering, anything after the box, will go over top of it. If it was at the end, it would be on the top, covering everything else up.

Author:  Insectoid [ Wed Mar 26, 2008 6:09 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

that doesn't work...I get a white screen.
hey, how do you post an example with turing:
instead of code:?

Author:  Sean [ Wed Mar 26, 2008 6:14 pm ]
Post subject:  Re: program 'end loop's before I tell it to

[syntax="Turing] then end it with [/syntax]

There was an extra end if in my example above, remove that and it might work. But, it should clear the entire screen then go back to your first put statement.

Author:  Insectoid [ Wed Mar 26, 2008 6:22 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

um, it worked fine until I put color in...your way has no color

Author:  Insectoid [ Wed Mar 26, 2008 6:25 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

wait, now everything's fine. I must of accidentally deleted something...
thanks for trying
hey! Im a newbie programmer now!

Author:  Sean [ Wed Mar 26, 2008 6:30 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Alright, glad it worked for you.

Author:  Insectoid [ Wed Mar 26, 2008 6:39 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

wait, never mind,after some more tests, It only works for some (again)
help?

Author:  Sean [ Wed Mar 26, 2008 6:43 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Post your revised copy, and I'll take a look see.

Author:  Insectoid [ Wed Mar 26, 2008 6:45 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Turing:

% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
setscreen ("graphics: 500; 100")
colorback (yellow)
cls
loop

    loop
        color (blue)
        put "This program will show you (inaccuratly)"
        put "How long you have left to live."

        % optional start
        put "would you like to know? (yes or no)"
        get seeage
        cls
        exit when seeage = "yes"
    end loop

    %input (age)
    loop
        put "Enter your age by typing on the keyboard."
        get age
        cls
        %calculations
        total := 81 - age

        % a message to the user based on age
        %ERROR only some work.
        put "you have ", total, " years left to live"
        if total < 0 then
            put "your dead!"
        elsif total > 0 and total < 20 then
            put "you haven't got much time left!"
        elsif total > 20 and total < 30 then
            put "getting on in years"
        elsif total > 30 and total < 40 then
            put "midlife crisis, anyone?"
        elsif total > 40 and total < 50 then
            put "Time to settle down"
        elsif total > 50 and total < 60 then
            put "your rebellious years are over. get used to it."
        elsif total > 60 and total < 0 then
            put "why do you care? you've got lots of time left!"
        elsif total = 0 then
            put "TIme to organize a funeral!"

        end if
        %restart the program.
        % ERROR Doesn't seem to work, always reverts to the beginning
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        cls
        exit when seeage = "no"


    end loop
end loop

Author:  Nick [ Wed Mar 26, 2008 6:48 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

get rid of the second internal loop and it should work perfect

Author:  Sean [ Wed Mar 26, 2008 6:48 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Which ones are exactly having the problem, and what are you inputing to have this problem?

Author:  Tallguy [ Thu Mar 27, 2008 8:33 am ]
Post subject:  Re: program 'end loop's before I tell it to

Quote:
loop
color (blue)
put "This program will show you (inaccuratly)"
put "How long you have left to live."

% optional start
put "would you like to know? (yes or no)"
get seeage
cls
exit when seeage = "yes"
end loop



i dont even hav to enter 'yes' for the program to start, its not a big deal, but make if the user puts in something other then 'yes' or 'no, put like thats not an option etc.[/quote]

Quote:
%restart the program.
% ERROR Doesn't seem to work, always reverts to the beginning

earse that section, delay it ands let the loop d its thing

Author:  Insectoid [ Thu Mar 27, 2008 11:25 am ]
Post subject:  RE:program \'end loop\'s before I tell it to

Tallguy, you do have to enter 'yes'. Anything else and it will just refrsh the screen.
Also, the 'restart program' section I would like to have, part of my 'learning process'
And don't quote my coding, read this: http://compsci.ca/v3/viewtopic.php?t=16370


Nick, without the second loop, it does exactly something I don't want it to do, and it dos not solve the problem I am having. Read all the posts next time, before responding. Othrwise ou are just wasting bandwidth.

sean-the issues:
These ones show age, but not the message
code:

  elsif total > 60 and total < 0 then
            put "why do you care? you've got lots of time left!"

elsif total > 50 and total < 60 then
            put "your rebellious years are over. get used to it."



every subsequent 'elsif' shows the message, but not the age.

Author:  Mackie [ Thu Mar 27, 2008 11:54 am ]
Post subject:  RE:program \'end loop\'s before I tell it to

code:
elsif total > 60 and total < 0 then
            put "why do you care? you've got lots of time left!"


Well, for this one, what you have it so a the total needs to be greater than 60 and less than 0, there is no number that meets those criteria. Maybe you meant 'or' rather than 'and'?

Author:  Sean [ Thu Mar 27, 2008 12:22 pm ]
Post subject:  Re: program 'end loop's before I tell it to

He didn't mean to add an 'and' or an 'or', due to the fact that he already has it checking for an age lower then zero.

What he wants to do, is to get rid of the rest of the code, leaving the part checking to see if the Total was higher then 65.

Turing:

elsif total > 60 then
            put "why do you care? you've got lots of time left!"

Author:  Insectoid [ Thu Mar 27, 2008 2:25 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Well, Sean, you fixed one problem...

I changed it a bit to this:
code:


% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
var enter : string
%introduction to program
setscreen ("graphics: 500; 100")
colorback (yellow)
cls
loop

    loop
        color (blue)
        put "This program will show you (inaccuratly)"
        put "How long you have left to live."

        % optional start
        put "would you like to know? (yes or no)"
        get seeage
        cls
        exit when seeage = "yes"
    end loop

    %input (age)
    loop
        put "Enter your age by typing on the keyboard."
        get age
        cls
        %calculations
        total := 81 - age

        % a message to the user based on age
        %ERROR only some work.
        put "you have ", total, " years left to live"
        put "press any button to continue"
        get enter
        cls
        if total < 0 then
            put "your dead!"
        elsif total > 0 and total < 20 then
            put "you haven't got much time left!"
        elsif total > 20 and total < 30 then
            put "getting on in years"
        elsif total > 30 and total < 40 then
            put "midlife crisis, anyone?"
        elsif total > 40 and total < 50 then
            put "Time to settle down"
        elsif total > 50 and total < 60 then
            put "your rebellious years are over. get used to it."
        elsif total > 60 then
            put "why do you care? you've got lots of time left!"
        elsif total = 0 then
            put "Time to organize a funeral!"

        end if
        %restart the program.
        % ERROR Doesn't seem to work, always reverts to the beginning
        put "Would you like to do it again, "
        put "so that you can lie about your age "
        put "and get a better score? "
        put "(yes or no)"
        get seeage
        cls
        exit when seeage = "no"


    end loop
end loop




I changed it so if you press any button (well, any button that isn't pre-assigned) and enter it will continue to display your message.
Is there any way to do it so the user only has to press enter, and not something else first?

Author:  Sean [ Thu Mar 27, 2008 2:38 pm ]
Post subject:  Re: program 'end loop's before I tell it to

Look up getch for what you want. It will halt the program until a button is pressed.

Or you can do this..

Turing:

put "Would you like to continue now? (Y/N)"
get enter
exit when enter(1) = "n" or enter(1) = "N"
cls

if enter(1) = "Y" or enter(1) = "y" then
%Your if statements
end if


But I suggest reading getch before using my idea, since you learn better by doing it yourself. Plus, what I wrote, might not be what you are after.

Author:  Insectoid [ Thu Mar 27, 2008 2:49 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

no, that isn't what I was after...I don't want it to be optional, just 'hit a button to conitnue'
so it pauses to let the user read it, then they can continue the program to see the rest. I don't want to use delay, either

Author:  Mackie [ Thu Mar 27, 2008 2:54 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

code:
put "This will wait for a user."
loop
    exit when hasch
end loop
put "Press a key for this to appear."


Perhaps?

Author:  Insectoid [ Thu Mar 27, 2008 3:06 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Actually, while reading a different thread, I found something called Input.Pause.
It waits for any input, then continues the program!
thanks anyway

Author:  Nick [ Thu Mar 27, 2008 3:10 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

the thign about Input.Pause is that it creates a flashing box where the text cursor is... Mackie's method is great if you want a subtle input pause

Author:  Insectoid [ Thu Mar 27, 2008 3:21 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Meh, that's okay, as long as it works!
Food for thought: My teacher has not taught any of the following, which I have discovered (with some help from you guys) how to use:
looping
'exit when'
multiple looping
if/then statements
Input.Pause
randint

Well, my teacher does say that I'm the best student he has ever had...

Author:  DaveAngus [ Fri Mar 28, 2008 11:49 am ]
Post subject:  RE:program \'end loop\'s before I tell it to

Here is your code updated to make it work a bit better

code:


% variable declarations
var age : int
var timeleft : int
var seeage : string
var total : int
var gender : string
%introduction to program
loop

    put "This program will show you (inaccuratly)"
    put "How long you have left to live."

    % optional start
    put "would you like to know? (yes or no)"
    get seeage
    exit when seeage = "no"


    %input (age)

    put "Enter your age by typing on the keyboard."
    get age

    %calculations
    total := 81 - age

    % a message to the user based on age
    %ERROR only some work.

    % BY DAVEANGUS
    % I changed this bottom part a bit. Just refer to your
    % origional to see what I did.
    % YOu have to put the "<=" if you want it to read it properly.
    %also at the end you had "lsif total > 60 and total < 0 then"
    % which confused the program
    % I changed that to <=80. It makes a bit more sense.


    put "you have ", total, " years left to live"
    if total < 0 then
        put "your dead!"
    elsif total <= 20 then
        put "you haven't got much time left!"
    elsif total <= 30 then
        put "getting on in years"
    elsif total <= 40 then
        put "midlife crisis, anyone?"
    elsif total <= 50 then
        put "Time to settle down"
    elsif total <= 60 then
        put "your rebellious years are over. get used to it."
    elsif total <= 80 then
        put "why do you care? you've got lots of time left!"
    elsif total = 0 then
        put "TIme to organize a funeral!"
    end if

    %restart the program.
    % ERROR Doesn't seem to work, always reverts to the beginning
    put "Would you like to do it again, "
    put "so that you can lie about your age "
    put "and get a better score? "
    put "(yes or no)"
    get seeage
    exit when seeage = "no"


end loop

%end with a little statement

put "Thanks a lot for playing"

Author:  Sean [ Fri Mar 28, 2008 11:53 am ]
Post subject:  Re: program 'end loop's before I tell it to

Dave yet again you post a copy of his work, completed by you. That is not what he wants, nor what we wish for you to do. We want him to learn, and not for him to feel the need to rely on others.

The thread was answered, so if a Mod can, please lock the thread.

Thank you.

Author:  DaveAngus [ Fri Mar 28, 2008 12:04 pm ]
Post subject:  RE:program \'end loop\'s before I tell it to

Sean,
no need to be so uptight.
Lay back.
Relax...
Im helping him out. alright?


: