i have so many functions in my program,what i want to do is, i want to return back to the main() but in somewhere middle of it!.

should i use goto statements?
i tried 'em,it gave some error!

if i should use goto then please explain how to,thanks

Recommended Answers

All 11 Replies

Member Avatar for iamthwee

I'm guessing you're a newbie.

There are a few justifications that warrant the use of gotos. I'm willing to bet this ain't one of them.

Post your code?

It's freeware in the public domain. Try this, it works:

struct Cry {
  const char* what() const {
    return "Mummy, I\'m afraid!";
  }
};
void Lots() { 2*2==4; throw Cry(); }
void of () { Lots(); }
void functions  () { of(); } 
int main() 
{
  try {
    functions();
  }
  catch(Cry mail) {
    cout << mail.what() << "..\n";
    cout << "I\'m very glad to see you!" << endl;
  }
  catch(...) {
    cout << "Bye forever ;(" << endl;
  }
  return 0;
}
Member Avatar for iamthwee

The above is proof that people who use c++ are geeks and have no life :P

ya i am a newbie!
i want to return to the lines above ,where i call the function,i cant design my program.
i want to return to a menu which is in the main and if the user wants to run the function again then he can select it again from the main

It's freeware in the public domain. Try this, it works:

struct Cry {
  const char* what() const {
    return "Mummy, I\'m afraid!";
  }
};
void Lots() { 2*2==4; throw Cry(); }
void of () { Lots(); }
void functions  () { of(); } 
int main() 
{
  try {
    functions();
  }
  catch(Cry mail) {
    cout << mail.what() << "..\n";
    cout << "I\'m very glad to see you!" << endl;
  }
  catch(...) {
    cout << "Bye forever ;(" << endl;
  }
  return 0;
}

thanks for posting,but can u plz explain a bit?

...but can u plz explain a bit?

Better look at http://www.cprogramming.com/tutorial/exceptions.html or search Google for C++ exception handling.
It's possible to use exceptions for the program control (however as usually it's considered as a bad style).
I think no special problems to design your menu handling to provide fast return to the main loop without gotos and exceptions...

goto's can only jump to labels within the same function,
so I don't think you will be able to do it with goto's if you are in some far away function.

There are any beautifull way of doing what you want without having to redesign you program or use c++ exceptions.

On a general note goto should be avoided.
I've only found them usefill in some crazy switch statements.
Which basicly changes it to a jump table.

i have functions that call "other functions",and i want to make a menu that return the user from the "other functions" to the main directly,but i also have a while loop in main,and some code above it,i want to goto the while loop from the "other functions"!

Reset your var..Cleanup..Then call menu..It's hard to read your codes..esspecialy if it invinsible..

Maybe you should just try to call 'return <something>' in your code ...
After the return instruction no code is executed anymore so the function will go back to 'main()' (if the function is called from the 'main()' function)

P.S. : Can you please post us some code so we can take a look at what exactly you do mean?

Reset your var..Cleanup..Then call menu..It's hard to read your codes..esspecialy if it invinsible..

lol.....here is my code,i want to add a choice to each menu so that the user can return to the main function whenever he wants to.

char *record(char **p,char **q,int n,int ho);
char *trans(char **p,char **q,int n,int ho);


char *trans(char **p,char **q,int n,int ho)
{
    int t;

    cout << "--------Transactions Menu------------" << endl << endl;

            cout << "To Add A Record,Press 1 " << endl;

            cout << "To Delete A Record,Press 2 " << endl;

            cout << "To Update A Record,Press 3 " << endl;

            cout << "To Go Back To Main Menu,Press 0 " << endl;



        cin >> t;

    switch(t)
    {
        case 0:
        {
            system("cls");
            // how to return to main's while loop???
            break;
        }
    case 1:
        {
            system("cls");
            record(p,q,n,ho);
            break;
        }
    case 2:
        {
            system("cls");
            del(p,q,n,ho);
            break;
        }
    case 3:
        {
            system("cls");

            break;
        }
    }


return 0;

}

char *record(char **p,char **q,int n,int ho)
{
    int r;

    cout << "-----------Record Menu------------" << endl << endl;

    cout << "To Add A Customer Record,Press 1 " << endl;

    cout << "To Delete A Hotel Record,Press 2 " << endl;

    cout << "To Go Back To Main Menu,Press 0 " << endl;

    cin>> r;



    switch(r)
    {
        case 0:
        {
            system("cls");
            // how to return to main's while loop???
            break;
        }
    case 1:
        {
            system("cls");
            addcustomer(p,n);
            break;
        }
    case 2:
        {
            system("cls");
            addhotel(q,ho);
            break;
        }

    }
return 0;

}
int _tmain()

{
    int choice,n,ho;

    cout << "Please Enter the number of records you want to Enter: " << endl;

    cin>>n;

    cout << "Please Enter The Number Of Rooms you want to Enter: " << endl;

    cin>>ho;



    char **p=new char*[n*8];
    cin.ignore();

    for(int i=0;i<n*8;i++)
    {
        p[i]=new char[20];
    }

    char **q=new char*[ho*8];
    //cin.ignore();

    for(int j=0;j<ho*5;j++)
    {
        q[j]=new char[20];
    }

int w=0;

while(w!=6)
{
//point:
    system("cls");


    cout << "------------------------- Hotel Database Management System------------------------------....................... Assignment 3_Question 4 .... \n................................Arshad Mehmood" << endl << endl;


    cout << "To Go to Transactions Menu,Press 1 " << endl;


    cout << "To Go to Queries Menu,Press 2 " << endl;


    cout << "To Go to Sorting Menu,Press 3 " << endl;


    cout << "To Search The Existing Record,Press 4 " << endl;


    cout << "To Show All Database Information,Press 5 " << endl;

    cout << "To Exit,Press 6 " << endl;

    cin >> choice;

    switch(choice)
    {
    case 1:
        {
            system("cls");
            trans(p,q,n,ho);
            break;
        }
    case 2:
        {
            system("cls");
            queries(p);
            break;
        }
    case 3:
        {
            system("cls");
            sort(p);
            break;
        }
    case 4:
        {
            system("cls");
            search(p);
            break;
        }
    case 5:
        {
            system("cls");
            showittome(p,q,n,ho);
            break;
        }
    case 6:
        {

            w=6;
            break;
        }


    }
}       
cout << endl << "Application Terminated" << endl;
    return 0;
}

hey br0 I solved it, I was wondering how to do this and when I got through ur code, I said wooo what a gr8 idea to use cls to clear screen and then it hit me... why shouldn't I use just main as function >> main ();

this is my function that I use to return to exit and to return to main, main function was not working till now... hh =)

#include "iNCLuDeS-mAiN.h"

int main ();

void povratak ()
{
    system ("color F0");
    int pov_odabir;
    do
    {
        cout << "Sto sada zelite ?\n1) Izaci iz programa.\n2) Vratiti se na pocetak programa." << endl;
        cin >> pov_odabir;
        if (pov_odabir == 1)
        {
            system ("exit");
        }
        else if (pov_odabir == 2)
        {
        system("cls");
        main ();
        }
        else
        {
        cout << "\nError - krivi unos !!!" << endl << endl;
        }
    }
    while ((pov_odabir > 2) || (pov_odabir < 1));
}

u just need to create header.h file and paste this in it, in main source just call function povratak ();to go to this menu and dont forget to include #include "header.h" in ur main source, with "" not <>...

Best Regards,
Cokaric

EDIT

#include "iNCLuDeS-mAiN.h" >>

#include <iostream>
#include <string>
#include <cstdlib>
#include <math.h>
using namespace std;
commented: 9 months too late. -1
commented: Leave dead threads in the graveyard! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.