Ahmad Raza
Ahmad Raza

Reputation: 145

Is there any function available to transfer the flow in c++ programming alternative to while or do while loop?

I am new to programming. & generally used to do programming on weekends. While working on a mini ATM project the problem arrives when i need to transfer the flow of the program back to up to the first line. i have already written the code of 1256 line so i can't re-structured it for while or do while loop.I searched for it a lot in the online portals but couldn't found a satisfactory results. My question is that is there any in-build function or way available for that cause.

my first line was.std::cout<<"Wlcome to your account \n"; Then my selecting option. std::cout<<"press 12 to go to main manue \n"; that was my else if statement from where i want to send back my flow towards the first line. else if (in.amount==12) { }

what could i write in that brackets to send back the flow of program to first line and the screen show's me agian "Welcome to your account"

Upvotes: 1

Views: 193

Answers (2)

v010dya
v010dya

Reputation: 5848

There is no need for a goto (which is a very bad practice in a high-level language). You can simply wrap your whole function in an infinite loop:

You had:

void foo()
{
  // code
  // you want to restart here
  // you want to quit here
  // code
}

You will have:

void foo()
{
  for(;;)
  {
    // code

    // you want to restart here
    continue;

    // you want to quit here
    break;

    // code

    break; // if you want to terminate at the end;
  }
}

Upvotes: 0

melpomene
melpomene

Reputation: 85767

i have already written the code of 1256 line so i can't re-structured it for while or do while loop.

Why not? You could just wrap a while loop around the whole thing.

That said, there is a way to do exactly what you're asking for: goto.

First you need to label one of your statements. For example:

int main() {
  the_beginning:
    std::cout << "Welcome to your account\n";
    ...
}

Then you can do goto the_beginning; to transfer control to the statement labeled the_beginning.

See goto on cppreference for more information and examples.

Upvotes: 4

Related Questions