-1
int main()
{
    Kund KundObjekt;
    Meny MenyObjekt;
    Konto KontoObjekt;


    MenyObjekt.Meny1();
    KundObjekt.LoggaIn();
    MenyObjekt.Meny2(KundObjekt);
    if (menyval2 == 4) 
    {

    }
    KontoObjekt.PengarIn();
    KontoObjekt.PengarUt();
    KontoObjekt.VisaSaldo();
    return 0;
    system("PAUSE");
}

How do I start at the top again if the if happens? Like if menyval2 == 4 I want the next call to be MenyObjekt.Meny1(); and then KundObjekt.LoggaIn(); and so on.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
filipanton1
  • 59
  • 1
  • 1
  • 9

4 Answers4

2

Use the while() loop, of course

while(menyval2 == 4)
{
    MenyObjekt.Meny1();
    KundObjekt.LoggaIn();
    MenyObjekt.Meny2(KundObjekt);
}

This loop will execute until the value stops being 4

Alternatively, if your logic demands that the 3 lines are executed regardless of the initial value of menyval2, you can use the do-while loop, as suggested by João Castilho.

I'll repeat his code here, for clarity:

do{
    MenyObjekt.Meny1();
    KundObjekt.LoggaIn();
    MenyObjekt.Meny2(KundObjekt);
}while(menyval2 == 4);
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
1

I think you want a do-while loop. In that case, the answer is

do{
    MenyObjekt.Meny1();
    KundObjekt.LoggaIn();
    MenyObjekt.Meny2(KundObjekt);
}while(menyval2 == 4);
João Castilho
  • 487
  • 4
  • 19
0

Use goto statement. Eg:

int main
{
   int X;
   A : 
       X = 10;
       if ( X== 10)
       {
           goto A;
        }
return 0;
}

that is all,When it comes under if statement . it will reach the top of if statement.

0

How do I start at the top again if the if happens?

You might consider the following minor refactor:

int main2()
{
    Kund KundObjekt;
    Meny MenyObjekt;
    Konto KontoObjekt;

    MenyObjekt.Meny1();
    KundObjekt.LoggaIn();
    MenyObjekt.Meny2(KundObjekt);
    if (menyval2 == 4) 
    {
       main2();  // you can invoke main2()
    }
    KontoObjekt.PengarIn();
    KontoObjekt.PengarUt();
    KontoObjekt.VisaSaldo();
    return 0;
}

int main()  // no code you write should invoke 'main'
{
   return (main2());
   system("PAUSE");
}

The idea is simply that you are never constrained to work within main().

Also, you must remember that the function 'main()' is special. Your code should never attempt to invoke it.

However, many of your peers won't like the recursion. For this to work, you will have to decide that the 'if-clause' is sufficient (to terminate the recursion).

On the other hand ...


Because you marked this as C++, you should consider using a class.

Using a class simplifies several issues.

For example, the functions f1() and f2() need no forward declare, nor is the order an issue.

Also, data attributes are available to all functions of the class instance, no need to pass values or be limited to the function scope where the data is declared.

class M2  
{
   private:
       Kund KundObjekt;
       Meny MenyObjekt;
       Konto KontoObjekt;       

   public:
       int exec()
       {
          do {   f1();  } while (menyval2 == 4); 
          f2();
          return(0);
       }

   private:  // note: no need to 'forward declare' for use in exec

      void f1() {
         MenyObjekt.Meny1();
         KundObjekt.LoggaIn();
         MenyObjekt.Meny2(KundObjekt);
      }

      void f2() {
        KontoObjekt.PengarIn();
        KontoObjekt.PengarUt();
        KontoObjekt.VisaSaldo();
      }
};

int main() 
{
   M2           m2;         // so create someplace to jump to
   int retVal = m2.exec();  // and do what you want there

   system("PAUSE");   
   return(retVal);
}

FYI - There exists a better mechanism than "system("PAUSE")". A tool setting change I think. Sorry, I do not know that tool.

2785528
  • 5,438
  • 2
  • 18
  • 20