C++ : One way jump to functions... any alternative? -


i student , learning c++. last year made simple project skul , realise make user friendly, made multiple calls 1 function without thinking of stack , memory leak cause. basically, student database management system , have made 1 function corresponding 1 screen i.e. 1 function menu, 1 adddata, modify data , on. code big have pasted small portion . observe how first called add1() menu() , again menu() add1()...

 `//***************************student database***********************************  void add1(){       system("cls");      char ch;      int i=no;       do{         i++;        cout<<"\nname : ";      cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );      getline (cin,s[i].name);            cout<<"\nclass : ";      cin>>s[i].cl;      cout<<"\nrollno : ";      cin>>s[i].rollno;      cout<<"\nmarks in";      s[i].m.avg=0;      for(int j=0;j<maxsb;j++)      {                             cout<<"\n"<<subject[j]<<": ";                             cin>>s[i].m.s[j];                             s[i].m.avg+=(s[i].m.s[j]/maxsb);                             }      no=i;      cout<<"\npress 'n' next , 'b' exit : ";       cin>>ch;      cout<<"\n***********************************************************************";      }while(ch=='n');       menu(); //calling menu() again function... }                              //*************         void menu(){      system("cls");      int n,flag=0;           {      cout<<"1.add data\n2.view report\n3.modify data\n4.delete data\n5.sort data\n6.go previous menu\n";      cout<<"please enter choice(enter corresponding integer): ";       cin>>n;      switch(n)      { case 2: report();break;        case 1: add1();break;        case 3: modify();break;        case 4: del();break;        case 5: sort();break;        case 6: first();         default:cout<<"please enter valid choice";system("cls");flag=1;        }        }        while(flag==1);                                    }                               //**************  void first() {      system("cls");      int n,flag=0;      cout<<"\t\t\tsoftware teachers";      cout<<"\n\n1.student database\n\n2.play game\n\n3.calculator";      cout<<"\n\nenter choice: ";      do{      flag=0;      cin>>n;      switch(n)      {               case 1:{  if(flag2==0)                          {           setting();                                     flag2=1;                                     }                         menu(); }                    break;               case 2: game();                    break;             //  case 3: calculator();                //    break;               default: cout<<"\nplz enter valid choice \n\n enter choice again:  ";flag=1;               }      }      while(flag); }  //****************************************************************************** int main() {     int temp;     first();     cin>>temp; } ` 

such jumping 1 function , never returning has been done several times in code, realize, kills stack.. have googled , know firstly not possible 1 way jump randomly 1 function , combining in single function , using goto jump 1 block isnt idea(its worse!!). so, question is, there better way can achieve task?? (this here means thing m trying in above code i.e. user friendliness of program)

thanks

instead of calling menu() or first() return previous menu, use return. example in menu() function:

   case 6: return;  

this means control return calling function right after function call. example when return add1 menu execution continue in loop asks choices.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -