Originally Posted by Slime
I'm sorry I'm very noob, I've learned a little bit of C just for this simple program. how do I move it to another function ??
thansk a lot!
generally if you want to continue something, but you don't have a fixed-number of iterations you want to put it through (you aren't aware of any bounds), you would put that function in a while loop until something "triggers" it to break out of the loop. in this case, the most basic thing you can do is to read for an input (such as you hitting ENTER after your main code is done executing), and evaluate it. in this case, if you hit ENTER, then that should tell the while loop to continue to go. if you hit anything else, then you can have it exit the program.
so for example, in a C program, in its most basic structure, it structured as:
// create your trigger here
int userDoesNotWantToExit = 1; // true
// enter the while loop (this is where you put your code that you want to repeat some indefinite time)
while( userDoesNotWantToExit )
// your code here
// after code is completed, you MUST update the condition yourself
// grab input from keyboard and have it check for a newline (to see if enter has been hit)
// else, exit the program
// this should be an if-else (if the input is newline, then 'userDoesNotWantToExit ' = 1, else 0)
// if the input is not a newline then the int is 0 and the while condition is not executed again
IF you want to execute something at least ONCE, then you would use a do-while as stated above. this guarantees that you'll at least perform some function (such as the first time a prompt for calculating some integers is displayed) once.
in this case though, the while loop is entirely equivalent because you're not changing the boolean value in between when its first declared, and when the function is actually called (there's no code in between that could possibly change the value before it goes into the while loop - nor anything in between that the while loop depends upon).Edited by 0x62 0x70 - 11/11/11 at 12:04pm