----------------------------------- compudave Wed Nov 12, 2003 11:00 pm 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. :) %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 ----------------------------------- poly Wed Nov 12, 2003 11:23 pm ----------------------------------- 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 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 Wed Nov 12, 2003 11:28 pm ----------------------------------- the problem is at when you had the elsif statement, you had 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: 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 Thu Nov 13, 2003 12:51 am ----------------------------------- 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 Thu Nov 13, 2003 5:02 pm ----------------------------------- 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 Fri Nov 14, 2003 1:32 pm ----------------------------------- I attend Catholic Central High. ----------------------------------- AsianSensation Fri Nov 14, 2003 4:54 pm ----------------------------------- cool, I go to Massey ----------------------------------- DBZ Wed Nov 19, 2003 8:26 pm ----------------------------------- 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! ----------------------------------- santabruzer Wed Nov 19, 2003 8:33 pm ----------------------------------- I agree with DBZ... you have to but and if statement for exit... for example: elsif input = "exit" then exit and somewhere outside the 'for' 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 Wed Nov 19, 2003 8:59 pm ----------------------------------- I dont think you guys should argue with AsianSensation... he got experience with turing. ... elseif input = "exit" then exit else ... is a better way. Exit when is really another way of having a separate if statment. ----------------------------------- AsianSensation Wed Nov 19, 2003 11:50 pm ----------------------------------- 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 Thu Nov 20, 2003 12:10 am ----------------------------------- Can't argue with the turing guru.... unfortuantly.. but i'll get there some day :D ... 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 Thu Nov 20, 2003 12:14 am ----------------------------------- heh :lol: 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. ----------------------------------- Andy Fri Nov 21, 2003 7:02 pm ----------------------------------- 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