By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

P: 1
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<fstream.h>
  5. class student
  6.     {
  7.     private:
  8.         int adm,scl;
  9.         char sname[15],ssec;
  10.     public:
  11.         void add(void)
  12.             {
  13.             clrscr();
  14.             cout << "\n\n\nEnter Student Information :";
  15.             cout << "\n\n Student No      :";
  16.             cin >> adm;
  17.             cout << "\n\n Student Name    :";
  18.             cin >> sname;
  19.             cout << "\n\n Student Class   :";
  20.             cin >> scl;
  21.             cout << "\n\n Student Section :";
  22.             cin >> ssec;
  23.             }
  24.         void mod(int no,int tstd,student *as)
  25.             {
  26.             }
  27.         void del(int no,int tstd,student *as)
  28.             {
  29.             }
  30.  
  31.         void vie(void)
  32.             {
  33.             }
  34.  
  35.     };
  36. class book
  37.     {
  38.     private:
  39.         int bno,bprice;
  40.         char bname[15],bauthor[15];
  41.     public:
  42.         void add(void)
  43.             {
  44.  
  45.             }
  46.         void mod(void)
  47.             {
  48.  
  49.             }
  50.         void del(void)
  51.             {
  52.  
  53.             }
  54.         void vie(void)
  55.             {
  56.  
  57.             }
  58.  
  59.     };
  60. void main()
  61. {
  62. int i,ch,sub,sinput;
  63. mainmenu:
  64. clrscr();
  65. cout << "                            Main Menu \n";
  66. cout << "                            ~~~~~~~~~\n\n\n\n";
  67. cout << "                          1.....Master\n\n";
  68. cout << "                          2.....Borrow Book\n\n";
  69. cout << "                          3.....Received Book\n\n";
  70. cout << "                          0.....Exit\n\n";
  71. cout << "                         ---------------------\n\n";
  72. cout << "                          Select :  ";
  73. cin >> ch;
  74. if (ch==1)
  75.     {
  76.     goto submenu;
  77.     }
  78. else if(ch==2)
  79.     {
  80.     goto borrowmenu;
  81.     }
  82. else if(ch==3)
  83.     {
  84.     goto receivemenu;
  85.  
  86.     }
  87. else if(ch==0)
  88.     {
  89.     cout << "\n\n\n                  Thanks for visit my project....";
  90.     getch();
  91.     return;
  92.     }
  93. else
  94.     {
  95.     cout << "\n\n\n                  Invalid choice try again : ";
  96.     getch();
  97.     goto mainmenu;
  98.     }
  99. submenu:
  100. clrscr();
  101. cout << "                            Submenu\n";
  102. cout << "                            ~~~~~~~\n\n\n\n";
  103. cout << "                          1...Student\n\n";
  104. cout << "                          2...Books\n\n";
  105. cout << "                          0...Back\n\n";
  106. cout << "                         --------------\n\n";
  107. cout << "                          Select : ";
  108. cin >> sub;
  109. if(sub==1)
  110.     {
  111.     goto studentmenu;
  112.     }
  113. else if(sub==2)
  114.     {
  115.     goto bookmenu;
  116.     }
  117. else if(sub==0)
  118.     {
  119.     goto mainmenu;
  120.     }
  121. else
  122.     {
  123.     cout << "\n\n                   Invalid choice :";
  124.     goto submenu;
  125.     }
  126.  
  127. studentmenu:
  128. clrscr();
  129. cout << "\n\n\n                          This is student menu :";
  130. cout << "\n\n                           1... Add student ";
  131. cout << "\n\n                           2....Modify Student";
  132. cout << "\n\n                           3....Delete Student";
  133. cout << "\n\n                           4....View Student";
  134. cout << "\n\n                           0....Back";
  135. cout << "\n\n                          ------------------------- ";
  136. cout << "\n\n                           select : ";
  137. cin >> sinput;
  138.  
  139. if(sinput==1)
  140.     {
  141.     goto addstudent;
  142.     }
  143. else if(sinput==2)
  144.     {
  145.     goto modifystudent;
  146.     }
  147. else if(sinput==3)
  148.     {
  149.     goto deletestudent;
  150.     }
  151. else if(sinput==4)
  152.     {
  153.     goto viewstudent;
  154.     }
  155. else if(sinput==0)
  156.     {
  157.     goto submenu;
  158.     }
  159. else
  160.     {
  161.     cout << "\n\n                   Invalid choice :";
  162.     goto studentmenu;
  163.     }
  164. goto submenu;
  165.  
  166. bookmenu:
  167. clrscr();
  168. cout << "\n\n\n                          This is book menu :";
  169. getch();
  170. goto submenu;
  171.  
  172. borrowmenu:
  173. clrscr();
  174. cout << "\n\n\n                          This is borrow menu :";
  175. getch();
  176. goto mainmenu;
  177.  
  178. receivemenu:
  179. clrscr();
  180. cout << "\n\n\n                          This is receive menu :";
  181. getch();
  182. goto mainmenu;
  183.  
  184.  
  185.  
  186.  
  187. addstudent:
  188. clrscr();
  189. cout << "\n\n\n                          This is Add New Student menu :";
  190. student s;
  191. s.add();
  192. ofstream obj1("std.txt");
  193. obj1.write((char *)&s,sizeof(s));
  194. obj1.close();
  195. getch();
  196. goto studentmenu;
  197.  
  198. modifystudent:
  199. clrscr();
  200. cout << "\n\n\n                          This is Modify Old Student menu :";
  201. getch();
  202. goto studentmenu;
  203.  
  204. deletestudent:
  205. clrscr();
  206. cout << "\n\n\n                          This is Delete Old Student menu :";
  207. getch();
  208. goto studentmenu;
  209.  
  210. viewstudent:
  211. clrscr();
  212. cout << "\n\n\n                          This is View Student menu :";
  213. cout << "\n--------------------------------------------------------------------------";
  214. getch();
  215. goto studentmenu;
  216. }
Attached Files
File Type: txt NL.txt (4.2 KB, 227 views)
Dec 5 '10 #1
Share this Question
Share on Google+
2 Replies


Savage
Expert 100+
P: 1,764
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.Generally, 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
Dec 5 '10 #2

Oralloy
Expert 100+
P: 983
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 "Unreachable 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
Dec 6 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.