Unreachable Code in C++ ?, When use my file management code, other wise all right

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tarun darji
    New Member
    • Dec 2010
    • 1

    Unreachable Code in C++ ?, When use my file management code, other wise all right

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    #include<fstream.h>
    class student
    	{
    	private:
    		int adm,scl;
    		char sname[15],ssec;
    	public:
    		void add(void)
    			{
    			clrscr();
    			cout << "\n\n\nEnter Student Information :";
    			cout << "\n\n Student No      :";
    			cin >> adm;
    			cout << "\n\n Student Name    :";
    			cin >> sname;
    			cout << "\n\n Student Class   :";
    			cin >> scl;
    			cout << "\n\n Student Section :";
    			cin >> ssec;
    			}
    		void mod(int no,int tstd,student *as)
    			{
    			}
    		void del(int no,int tstd,student *as)
    			{
    			}
    
    		void vie(void)
    			{
    			}
    
    	};
    class book
    	{
    	private:
    		int bno,bprice;
    		char bname[15],bauthor[15];
    	public:
    		void add(void)
    			{
    
    			}
    		void mod(void)
    			{
    
    			}
    		void del(void)
    			{
    
    			}
    		void vie(void)
    			{
    
    			}
    
    	};
    void main()
    {
    int i,ch,sub,sinput;
    mainmenu:
    clrscr();
    cout << "                            Main Menu \n";
    cout << "                            ~~~~~~~~~\n\n\n\n";
    cout << "                          1.....Master\n\n";
    cout << "                          2.....Borrow Book\n\n";
    cout << "                          3.....Received Book\n\n";
    cout << "                          0.....Exit\n\n";
    cout << "                         ---------------------\n\n";
    cout << "                          Select :  ";
    cin >> ch;
    if (ch==1)
    	{
    	goto submenu;
    	}
    else if(ch==2)
    	{
    	goto borrowmenu;
    	}
    else if(ch==3)
    	{
    	goto receivemenu;
    
    	}
    else if(ch==0)
    	{
    	cout << "\n\n\n                  Thanks for visit my project....";
    	getch();
    	return;
    	}
    else
    	{
    	cout << "\n\n\n                  Invalid choice try again : ";
    	getch();
    	goto mainmenu;
    	}
    submenu:
    clrscr();
    cout << "                            Submenu\n";
    cout << "                            ~~~~~~~\n\n\n\n";
    cout << "                          1...Student\n\n";
    cout << "                          2...Books\n\n";
    cout << "                          0...Back\n\n";
    cout << "                         --------------\n\n";
    cout << "                          Select : ";
    cin >> sub;
    if(sub==1)
    	{
    	goto studentmenu;
    	}
    else if(sub==2)
    	{
    	goto bookmenu;
    	}
    else if(sub==0)
    	{
    	goto mainmenu;
    	}
    else
    	{
    	cout << "\n\n                   Invalid choice :";
    	goto submenu;
    	}
    
    studentmenu:
    clrscr();
    cout << "\n\n\n                          This is student menu :";
    cout << "\n\n                           1... Add student ";
    cout << "\n\n                           2....Modify Student";
    cout << "\n\n                           3....Delete Student";
    cout << "\n\n                           4....View Student";
    cout << "\n\n                           0....Back";
    cout << "\n\n                          ------------------------- ";
    cout << "\n\n                           select : ";
    cin >> sinput;
    
    if(sinput==1)
    	{
    	goto addstudent;
    	}
    else if(sinput==2)
    	{
    	goto modifystudent;
    	}
    else if(sinput==3)
    	{
    	goto deletestudent;
    	}
    else if(sinput==4)
    	{
    	goto viewstudent;
    	}
    else if(sinput==0)
    	{
    	goto submenu;
    	}
    else
    	{
    	cout << "\n\n                   Invalid choice :";
    	goto studentmenu;
    	}
    goto submenu;
    
    bookmenu:
    clrscr();
    cout << "\n\n\n                          This is book menu :";
    getch();
    goto submenu;
    
    borrowmenu:
    clrscr();
    cout << "\n\n\n                          This is borrow menu :";
    getch();
    goto mainmenu;
    
    receivemenu:
    clrscr();
    cout << "\n\n\n                          This is receive menu :";
    getch();
    goto mainmenu;
    
    
    
    
    addstudent:
    clrscr();
    cout << "\n\n\n                          This is Add New Student menu :";
    student s;
    s.add();
    ofstream obj1("std.txt");
    obj1.write((char *)&s,sizeof(s));
    obj1.close();
    getch();
    goto studentmenu;
    
    modifystudent:
    clrscr();
    cout << "\n\n\n                          This is Modify Old Student menu :";
    getch();
    goto studentmenu;
    
    deletestudent:
    clrscr();
    cout << "\n\n\n                          This is Delete Old Student menu :";
    getch();
    goto studentmenu;
    
    viewstudent:
    clrscr();
    cout << "\n\n\n                          This is View Student menu :";
    cout << "\n--------------------------------------------------------------------------";
    getch();
    goto studentmenu;
    }
    Attached Files
    Last edited by Dormilich; Dec 6 '10, 09:21 AM. Reason: fixed [CODE] [/CODE] tags
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    I dont know what you mean with "my file managment code, otherwise its fine", but I do see a lot of goto's which is a bad thing, since they ruin program flow and make it less readable.Genera lly, you can write every structured programme without using goto's and in most cases you should go that way.So use goto's only when they are necessary (which is a rare thing) and use other language constructs to structure your code in a more readable form.

    Also, this might be a good read :)

    Regards

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      Tarun,

      C and C++ both have a switch statement. Use of the statement can really make your life easier.

      Savage pointed out the "goto" issue your code has; but that's not what's causing you grief at the moment.

      To find the "Unreachabl e Code", read your complier's error message and go to the line it calls out. The problem is likely the line immediately preceeding.

      Luck!
      Orally

      Comment

      Working...