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

Username:   Password: 
 RegisterRegister   
 Loop Execution Program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
compudave




PostPosted: Wed Nov 12, 2003 11:00 pm   Post subject: Loop Execution Program

I can't believe I actually got this far in my programming question anyways I'm having trouble with this program. I can't get it to exit when the user types "exit". Some help on my error please. Sorry 'bout the comments, I just like to be detailed when I type them. Smile

code:
%The Loop Executing Program - A program that annoucnes at each repetition of a loop
%the number of times it has executed the loop. It will stop at each execution with
%the message type "more" to continue.
%Written by David
%11.12.03

%Declaring 'total' as an integer variable and has an initial value of '0'.

var total : int := 0

%Declaring 'input' and 'more' as string variables.

var input, more : string

%Starting the loop

loop

%Starts the count at 1 and goes up to 1 million.

    for count : 1..1000000

%Displays that the program has 'count' (so many) times. Then tells the user to type more if
%they want to continue. Type exit if you want to exit the program.

        put "The program has run ", count, " time(s). Type 'more' to continue. Type 'exit' to stop."

%Total becomes count added to total. This will act as a storage for the
count and it will
%increase with each 'more' the user types.

        total := total + count
       
%Gets 'input' from the user.

        get input
       
%If the input equals 'more' then it will display a message and clear screen.

        if input = "more" then put "You are cool! You typed more." cls

%Else if the input equals itself then it will play a file, display a message,
%tell the user to type 'more' and not 'input' (that's whatever the user typed).
%Then it displays another message informing the program will still run.

        elsif input = input then Music.PlayFile ("C:/WINDOWS/MEDIA/CHORD.wav")
       
        put "Error! You were supposed to type 'more' to continue. Not ", input,"!"
       
        put "It doesn't matter anyways the program will still run..."

%Else exit.

        else exit

%End the if statement.

        end if
       
%Ends the for count.

    end for

%Ends the entire loop.

end loop
Sponsor
Sponsor
Sponsor
sponsor
poly




PostPosted: Wed Nov 12, 2003 11:23 pm   Post subject: (No subject)

I Just read over your code and you have a variable called MORE and its not even used in the program. Here is a loop program that I have lieing around my PC from last years programming, you can get an idea on how its done. In my example though if user types word longer than 4 characters than program crashes
code:
var total : int := 1    % Total Number of times loop goes through (starts at 1)
var count : int := 1    % Keeps track of count
var input : string (4)  % Input can only be 4 characters

loop
%Outputs total times loop went through
    put "Program has run ", total, " time(s). Type 'more' to continue. Type 'exit' to stop."
    get input
    % If user types EXIT than exit loop which ends program anything else it will keep loop going
    if input = "exit" then
        exit
    else
        total := total + count
    end if
end loop
AsianSensation




PostPosted: Wed Nov 12, 2003 11:28 pm   Post subject: (No subject)

the problem is at when you had the elsif statement, you had

code:
elsif input = input then


of course the input will always equal to the input, because it checks to see if "more" is the input, and then if it's not, it goes into the elsif statment, and it will always be true.

therefore, do this:
code:
    if input = "more" then
        put "You are cool! You typed more."
        cls
    elsif input = "exit" then
        exit
    else
        put "Error! You were supposed to type 'more' to continue. Not ", input, "!"
        put "It doesn't matter anyways the program will still run..."
    end if


this will check for the 2 cases, whether if the input is "more" or "exit", and if it's not any of those, then it display the error msg.

I don't get why you need the loop end loop there, you can run this program fine without using the infinite loop, either way, you only need one loop, the for loop, or the infinite loop.
compudave




PostPosted: Thu Nov 13, 2003 12:51 am   Post subject: (No subject)

yeah, I knew the problem was with my elsif statements. Man it's been a while since I've done this. I took out the initiating loop and it works fine now. Thanks a lot guys.

Asian Pride man:lol:
AsianSensation




PostPosted: Thu Nov 13, 2003 5:02 pm   Post subject: (No subject)

lol, Asian Pride indeed

here, have some bits.

Give compudave some bits

btw, what school do you go to in Windsor? I'm from Windsor too
compudave




PostPosted: Fri Nov 14, 2003 1:32 pm   Post subject: (No subject)

I attend Catholic Central High.
AsianSensation




PostPosted: Fri Nov 14, 2003 4:54 pm   Post subject: (No subject)

cool, I go to Massey
DBZ




PostPosted: Wed Nov 19, 2003 8:26 pm   Post subject: (No subject)

hey i looked at ur program and u haven't told the computer when to exit. you've just used the if statement and said that else if exit. Why don't u tyr using the 'exit when' statement? hope it helps!
Sponsor
Sponsor
Sponsor
sponsor
santabruzer




PostPosted: Wed Nov 19, 2003 8:33 pm   Post subject: (No subject)

I agree with DBZ... you have to but and if statement for exit... for example:
code:
elsif input = "exit" then exit


and somewhere outside the 'for'

code:
exit when input = "exit"


I don't understand why you use a for statement.. it would be much easier using a loop with a counter...

Santabruzer
Tony




PostPosted: Wed Nov 19, 2003 8:59 pm   Post subject: (No subject)

I dont think you guys should argue with AsianSensation... he got experience with turing.

code:

...
elseif input = "exit" then
     exit
else
...


is a better way. Exit when is really another way of having a separate if statment.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
AsianSensation




PostPosted: Wed Nov 19, 2003 11:50 pm   Post subject: (No subject)

santabruzer wrote:

I don't understand why you use a for statement.. it would be much easier using a loop with a counter...


lol, isn't a For just a Loop with a counter and an automatic exit condition? (exit when the counter in at certain place)

anyways, I guess I could have done that, but saving one line of coding space and not actually making the program more efficient isn't much to argue over isn't it? Besides, it took me less time to change his code than to write new ones

yeah, I know, I'm a lazy bum 8)
santabruzer




PostPosted: Thu Nov 20, 2003 12:10 am   Post subject: (No subject)

Can't argue with the turing guru.... unfortuantly.. but i'll get there some day Very Happy ... if i don't get to VB first... anywho, for statements are a loop with a counter, but i guess it's just a personal preferance.. i think loops are easy and more effective.
Tony




PostPosted: Thu Nov 20, 2003 12:14 am   Post subject: (No subject)

heh Laughing You're better off with VB. Trust me on this.

As for the loops vs forloops... well its preaty much the same really. But forloops look more organized and easier to understand when reading the code.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Andy




PostPosted: Fri Nov 21, 2003 7:02 pm   Post subject: (No subject)

here is a tip, never learn turing in the first place! go straight to vb or c++, if u want to major in compsci in university, you dont even need a compsci credit from high school
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  [ 14 Posts ]
Jump to:   


Style:  
Search: