Visual C++ to end a loop & exit from program?
Hi,
I want to give user an option to quit the running loop & exit the program whenever he wants. Well i have a function to accept data from user in loop & want to give option to user so he can stop the loop & get out of the function or the program!
I don't know how to achieve this?
Re: Visual C++ to end a loop & exit from program?
HEy you can put your condition in else block where you can define if the input == quit or no. 404 etc. then put your exit condition
Notice one thing if you are accepting the value in your function as an integer or variable?
I think this way you can end your loop easily!
Hope this helps! :)
Re: Visual C++ to end a loop & exit from program?
On occasion, it may be necessary to end a program (or section of a program) earlier than its normal termination. Below are examples of methods of interrupting or terminating programs or loops.
1. USE return 0;
It returns a value of 0 to the IDE indicating that the program reached normal termination.
2. USE exit( );
the exit( ) function also ends a program before its normal termination. It requires the Standard Library header file, stdlib.h. The format is exit(value);
where value is an integer variable or value.
Using exit(1); returns a value of 1 to the IDE indicating that an error must have occurred. This process is often used for error trapping.
3. USE break;
The break statement gets you out of a loop. No matter what the loop's ending condition, break immediately says "I'm outta here!" The program continues with the next statement immediately following the loop. Break stops only the loop in which it resides. It does not break out of a "nested loop" (a loop within a loop).
I recommend you use break; statement to terminate the loop! :)
Re: Visual C++ to end a loop & exit from program?
A Loop with an Exit Condition
I hope this will make you understand the concept in much batter way to exit or terminate the loop!
Re: Visual C++ to end a loop & exit from program?
The solution is simple, and is based on the fact that Visual Basic supports as many as three different kinds of loops: For, Do, and While. Each kind of loop supports a corresponding Exit keyword (Exit For, Exit Do, and Exit While), thus you can rewrite the code as follows:
Code:
Dim i As Integer = 1
Do While i <= 10
For j As Integer = 1 To 20
If Evaluate(i, j) = 0 Then Exit Do
' Do something here
Next
i += 1
Loop
You can use the same technique when you have up to three nested loops.
Incidentally, you can't adopt this technique in C#, because its break statement doesn't have the same "semantics power" of the Exit keyword in VB.