Is there a way to write such program without using goto? If so, how?

"Is there a way to write such program without using goto?"
-- Philip Moreira
Yes. Technically speaking, C++ is flexible enough that you can forbid pretty much any one construct and still be able to implement what you want, including goto.
The "how" part is a bit uninteresting, so let's just say you'd do that by taking two loops, using additional flag variables in one of the conditions so it doesn't behave like a loop anymore, and carefully use if to end up with something that meets the specification.
"Yes, but is there a way to naturally model this flowchart with only high-level flow control?"
-- Helpful voice-over
No.
This is actually one of the canonical cases where C++'s high-level constructs are not enough. All of them are designed around a concept of nested scopes (exception handling can cut a scope short, but that's it). This flowchart features two crossed loops, with just enough operations in-between that you can't just shuffle them around to get nested loops.
You have two choices:
Just use goto. Document it, make the label explicit, and apply other usual good practices.
Try to stick to the "no goto" rule to hell and forth, bending other control structures out of shape until you have effectively emulated a goto, with a side serving of rogue booleans and tricky conditions.
The second solution might end up more legible than the first (never say "never", they say), but you definitely shouldn't discard the first one before trying.
For reference, here is how you would translate your flowchart (actions and conditions numbered from their order in the flowchart):
action_one;
properlyNamedLabel:
action_two;
do {
action_three;
if(condition_one)
goto properlyNamedLabel;
action_four;
} while(condition_two);
action_five;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With