help me find the error

Reply

Join Date: Nov 2004
Posts: 14
Reputation: Tresa is an unknown quantity at this point 
Solved Threads: 0
Tresa Tresa is offline Offline
Newbie Poster

help me find the error

 
-1
  #1
Nov 5th, 2004
hai

can anyone please help me find the error in my program? when i try to compile it says case bypasses initialisation of local variable. i can't figure out the mistake. i am sitting with this for a week. i will be thankful if someone could help me figure it out. thank u.

Tresa

  1. #include<fstream.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6. #include <iomanip.h>
  7. #include<process.h>
  8. struct std
  9. {
  10. char city[26];
  11. };
  12. class student
  13. {
  14. std std;
  15. char name[20],phno[6];
  16. public:
  17. char gr[6];
  18. void getdata();
  19. void showdata1();
  20. void showdata2();
  21. void showdata3();
  22. char *getname()
  23. {
  24. return name;
  25. }
  26. char *getgr()
  27. {
  28. return gr;
  29. }
  30. void update(char *nm,char *telno)
  31. {
  32. strcpy(name,nm);
  33. strcpy(phno,telno);
  34. }
  35. void updateg(char *nm,char *grno)
  36. {
  37. strcpy(name,nm);
  38. strcpy(gr,grno);
  39. }
  40. void updatec(char *nm,char *c)
  41. {
  42. strcpy(name,nm);
  43. strcpy(std.city,c);
  44. }
  45. };
  46. void student :: getdata()
  47. {
  48. clrscr();
  49. cout<<"Add Student Data\n\n";
  50. cout<<"\nEnter Name : ";
  51. cin>>name;
  52. cout<<"Enter the gr no: ";
  53. cin>>gr;
  54. cout<<"Enter the city. ";
  55. cin>>std.city;
  56. cout<<"Enter Phone No. : ";
  57. cin>>phno;
  58. }
  59. void student :: showdata1()
  60. {
  61. cout<<"\n";
  62. cout<<" Name: "<<setw(10)<<name;
  63. cout<<endl;
  64. cout<<" Grno:"<<setw(10)<<gr;
  65. cout<<endl;
  66. cout<<" Phone: "<<setw(10)<<phno;
  67. cout<<endl;
  68. cout<<" City:"<<setw(10)<<std.city;
  69. cout<<endl;
  70. }
  71. void student :: showdata2()
  72. {
  73. cout<<"\n";
  74. cout<<" Name: "<<setw(10)<<name;
  75. cout<<endl;
  76. cout<<" Phone: "<<setw(10)<<phno;
  77. cout<<endl;
  78. }
  79. void student :: showdata3()
  80. {
  81. cout<<"\n";
  82. cout<<" Name: "<<setw(10)<<name;
  83. cout<<endl;
  84. cout<<"Grno:"<<setw(10)<<gr;
  85. cout<<endl;
  86. }
  87. int main()
  88. {
  89. student rec;
  90. char fname[15];
  91. int i,n;
  92. long int pass;
  93. fstream file;
  94. char ch,nm[20],telno[6],grno[6],c[26];
  95. int found;
  96. int choice,cho;
  97. while(1)
  98. {
  99. clrscr();
  100. cout<<"\n*****Phone Book*****\n";
  101. cout<<"1) Add New Record\n";
  102. cout<<"2) Display All Records\n";
  103. cout<<"3) Search.\n";
  104. cout<<"4) Update. \n";
  105. cout<<"5) Exit.\n";
  106. cout<<"Choose your choice : ";
  107. cin>>choice;
  108. switch(choice)
  109. {
  110. case 1 : //New Record
  111. clrscr();
  112. cout<<"enter the section.";
  113. cin>>fname;
  114. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  115. cin.get(ch);
  116. cout<<"enter how many students.";
  117. cin>>n;
  118. for(i=0;i<n;i++)
  119. {
  120. rec.getdata();
  121. file.write((char *) &rec, sizeof(rec));
  122. }
  123. file.close();
  124. break;
  125. case 2 : //Display All Records
  126. clrscr();
  127. cout<<"enter the section.";
  128. cin>>fname;
  129. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  130. file.seekg(0,ios::beg);
  131. found=0;
  132. cout<<"enter password.";
  133. cin>>pass;
  134. if(pass==6700765)
  135. {
  136. while(file.read((char *) &rec, sizeof(rec)))
  137. {
  138. cout<<"\n\nRecords \n";
  139. if(!file.eof())
  140. {
  141. found=1;
  142. rec.showdata1();
  143. }
  144. }
  145. }
  146. file.clear();
  147. if(found==0)
  148. {
  149. cout<<"\n\n---Record Not found---\n";
  150. getch();
  151. }
  152. file.close();
  153. getch();
  154. break;
  155. case 3 : //search
  156. clrscr();
  157. cout<<"Menu.\n";
  158. cout<<"1.search for a telephone no.\n";
  159. cout<<"2.search for a person.\n";
  160. cout<<"enter choice.\n";
  161. cin>>cho;
  162. switch(cho)
  163. {
  164. case 1:// to search a telephoneno.
  165. clrscr();
  166. cout<<"enter the section.";
  167. cin>>fname;
  168. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  169. cout<<"\n\nEnter Name : ";
  170. cin>>nm;
  171. file.seekg(0,ios::beg);
  172. found=0;
  173. while(file.read((char *) &rec, sizeof(rec)))
  174. {
  175. if(strcmp(nm,rec.getname())==0)
  176. {
  177. found=1;
  178. rec.showdata2();
  179. }
  180. }
  181. file.clear();
  182. if(found==0)
  183. cout<<"\n\n---Record Not found---\n";
  184. getch();
  185. file.close();
  186. break;
  187. case 2:// to search for a person.
  188. clrscr();
  189. cout<<"enter the section.";
  190. cin>>fname;
  191. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  192. cout<<"\n\nEnter gr no : ";
  193. cin>>grno;
  194. file.seekg(0,ios::beg);
  195. found=0;
  196. while(file.read((char *) &rec, sizeof(rec)))
  197. {
  198. if(strcmp(grno,rec.getgr())==0)
  199. {
  200. found=1;
  201. rec.showdata3();
  202. }
  203. }
  204. file.clear();
  205. if(found==0)
  206. cout<<"\n\n---Record Not found---\n";
  207. getch();
  208. file.close();
  209. break;
  210. }
  211. case 4 : //Update
  212. clrscr();
  213. cout<<"***** Menu *****\n\n";
  214. cout<<"1.Telephone no.\n";
  215. cout<<"2.Grno.\n";
  216. cout<<"3.City.\n";
  217. cout<<"Enter the choice.\n";
  218. cin>>cho;
  219. switch(cho)
  220. {
  221. case 1://update telephone no.
  222. clrscr();
  223. cout<<"enter the section.";
  224. cin>>fname;
  225. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  226. cout<<"\n\nEnter Name : ";
  227. cin>>nm;
  228. file.seekg(0,ios::beg);
  229. found=0;
  230. int cnt=0;
  231. while(file.read((char *) &rec, sizeof(rec)))
  232. { // why is this case not woking?
  233. cnt++;
  234. if(strcmp(nm,rec.getname())==0)
  235. {
  236. found=1;
  237. break;
  238. }
  239. }
  240. file.clear();
  241. if(found==0)
  242. cout<<"\n\n---Record Not found---\n";
  243. else
  244. {
  245. int location = (cnt-1) * sizeof(rec);
  246. cin.get(ch);
  247. if(file.eof())
  248. file.clear(); // for what is this used?
  249. cout<<"Enter New Telephone No : ";
  250. cin>>telno;
  251. file.seekp(location);
  252. rec.update(nm,telno);
  253. file.write((char *) &rec, sizeof(rec));
  254. file.flush();
  255. file.close();
  256. }
  257. clrscr();
  258. break;
  259. case 2: //Update Gr No. //why is this case not working?
  260. clrscr(); //if the above case is not commented
  261. cout<<"enter the section."; //the error is coming as case bypasses
  262. cin>>fname; //initialization of a local variable.
  263. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  264. cout<<"\n\nEnter Name : ";
  265. cin>>nm;
  266. file.seekg(0,ios::beg);
  267. found=0;
  268. int cn=0;
  269. while(file.read((char *) &rec, sizeof(rec)))
  270. {
  271. cn++;
  272. if(strcmp(nm,rec.getname())==0)
  273. {
  274. found=1;
  275. break;
  276. }
  277. }
  278. file.clear();
  279. if(found==0)
  280. cout<<"\n\n---Record Not found---\n";
  281. else
  282. {
  283. int location = (cn-1) * sizeof(rec);
  284. cin.get(ch);
  285. if(file.eof())
  286. file.clear();
  287. cout<<"Enter New Gr No : ";
  288. cin>>grno;
  289. file.seekp(location);
  290. rec.updateg(nm,grno);
  291. file.write((char *) &rec, sizeof(rec));
  292. file.flush();
  293. file.close();
  294. }
  295. break;
  296. case 3: //Update Gr No. //why is this case not working?
  297. clrscr(); //if the above case is not commented
  298. cout<<"Enter the section."; //the error is coming as case bypasses
  299. cin>>fname; //initialization of a local
  300. //variable.
  301. file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
  302. cout<<"\n\nEnter Name : ";
  303. cin>>nm;
  304. file.seekg(0,ios::beg);
  305. found=0;
  306. int cn1=0;
  307. while(file.read((char *) &rec, sizeof(rec)))
  308. {
  309. cn1++;
  310. if(strcmp(nm,rec.getname())==0)
  311. {
  312. found=1;
  313. break;
  314. }
  315. }
  316. file.clear();
  317. if(found==0)
  318. cout<<"\n\n---Record Not found---\n";
  319. else
  320. {
  321. int location = (cn-1) * sizeof(rec);
  322. cin.get(ch);
  323. if(file.eof())
  324. file.clear();
  325. cout<<"Enter New city : ";
  326. cin.getline(c,25);
  327. file.seekp(location);
  328. rec.updatec(nm,c);
  329. file.write((char *) &rec, sizeof(rec));
  330. file.flush();
  331. }
  332. file.close();
  333. break;
  334. }
  335. getch();
  336. case 5:
  337. exit(0);
  338. }
  339. }
  340. }
Last edited by alc6379; Nov 7th, 2004 at 5:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: help me find the error

 
0
  #2
Nov 5th, 2004
Phew! A huge main with lots of switches!

The compiler warning means, though, that there is a theoretical path through your switch statements that does not initialize one of the local variables you declared in main. A simple solution is to initialize your local variables when you declare them to shut up the compiler. Here's a simplified example of what is happening:

  1. bool proc( int val )
  2. {
  3. int localVariable;
  4. switch (val)
  5. {
  6. case 1:
  7. localVariable = 1;
  8. break;
  9. default:
  10. if (localVariable == 1) return true;
  11. break;
  12. }
  13. return false;
  14. }

What happens if 'val' is not one? The compiler warns you that localVariable may not have been initialized in the default case.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 14
Reputation: Tresa is an unknown quantity at this point 
Solved Threads: 0
Tresa Tresa is offline Offline
Newbie Poster

Re: help me find the error

 
0
  #3
Nov 6th, 2004
thank u so much chainsaw.

Tresa
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: help me find the error

 
0
  #4
Nov 7th, 2004
Can you do us a favor?

In the future, when posting any code, especially such large snippets, please put it in vB code tags, for the sake of readibility.

Thanks!
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC