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.