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

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip.h>
#include<process.h>
struct std
{
char city[26];
};
class student
{
std std;
char name[20],phno[6];
public:
char gr[6];
void getdata();
void showdata1();
void showdata2();
void showdata3();
char *getname()
{
return name;
}
char *getgr()
{
return gr;
}
void update(char *nm,char *telno)
{
strcpy(name,nm);
strcpy(phno,telno);
}
void updateg(char *nm,char *grno)
{
strcpy(name,nm);
strcpy(gr,grno);
}
void updatec(char *nm,char *c)
{
strcpy(name,nm);
strcpy(std.city,c);
}
};
void student :: getdata()
{
clrscr();
cout<<"Add Student Data\n\n";
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter the gr no: ";
cin>>gr;
cout<<"Enter the city. ";
cin>>std.city;
cout<<"Enter Phone No. : ";
cin>>phno;
}
void student :: showdata1()
{
cout<<"\n";
cout<<" Name: "<<setw(10)<<name;
cout<<endl;
cout<<" Grno:"<<setw(10)<<gr;
cout<<endl;
cout<<" Phone: "<<setw(10)<<phno;
cout<<endl;
cout<<" City:"<<setw(10)<<std.city;
cout<<endl;
}
void student :: showdata2()
{
cout<<"\n";
cout<<" Name: "<<setw(10)<<name;
cout<<endl;
cout<<" Phone: "<<setw(10)<<phno;
cout<<endl;
}
void student :: showdata3()
{
cout<<"\n";
cout<<" Name: "<<setw(10)<<name;
cout<<endl;
cout<<"Grno:"<<setw(10)<<gr;
cout<<endl;
}
int main()
{
student rec;
char fname[15];
int i,n;
long int pass;
fstream file;
char ch,nm[20],telno[6],grno[6],c[26];
int found;
int choice,cho;
while(1)
{
clrscr();
cout<<"\n*****Phone Book*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search.\n";
cout<<"4) Update. \n";
cout<<"5) Exit.\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice)
{
case 1 : //New Record
clrscr();
cout<<"enter the section.";
cin>>fname;
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cin.get(ch);
cout<<"enter how many students.";
cin>>n;
for(i=0;i<n;i++)
{
rec.getdata();
file.write((char *) &rec, sizeof(rec));
}
file.close();
break;
case 2 : //Display All Records
clrscr();
cout<<"enter the section.";
cin>>fname;
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
file.seekg(0,ios::beg);
found=0;
cout<<"enter password.";
cin>>pass;
if(pass==6700765)
{
while(file.read((char *) &rec, sizeof(rec)))
{
cout<<"\n\nRecords \n";
if(!file.eof())
{
found=1;
rec.showdata1();
}
}
}
file.clear();
if(found==0)
{
cout<<"\n\n---Record Not found---\n";
getch();
}
file.close();
getch();
break;
case 3 : //search
clrscr();
cout<<"Menu.\n";
cout<<"1.search for a telephone no.\n";
cout<<"2.search for a person.\n";
cout<<"enter choice.\n";
cin>>cho;
switch(cho)
{
case 1:// to search a telephoneno.
clrscr();
cout<<"enter the section.";
cin>>fname;
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata2();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
file.close();
break;
case 2:// to search for a person.
clrscr();
cout<<"enter the section.";
cin>>fname;
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cout<<"\n\nEnter gr no : ";
cin>>grno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(grno,rec.getgr())==0)
{
found=1;
rec.showdata3();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
file.close();
break;
}
case 4 : //Update
clrscr();
cout<<"***** Menu *****\n\n";
cout<<"1.Telephone no.\n";
cout<<"2.Grno.\n";
cout<<"3.City.\n";
cout<<"Enter the choice.\n";
cin>>cho;
switch(cho)
{
case 1://update telephone no.
clrscr();
cout<<"enter the section.";
cin>>fname;
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{                                                       // why is this case not woking?
cnt++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear(); // for what is this used?
cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
file.flush();
file.close();
}
clrscr();
break;
case 2: //Update Gr No.               //why is this case not working?
clrscr();                     //if the above case is not commented
cout<<"enter the section.";    //the error is coming as case bypasses
cin>>fname;                      //initialization of a local variable.
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cn=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cn++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cn-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear(); 
cout<<"Enter New Gr No : ";
cin>>grno;
file.seekp(location);
rec.updateg(nm,grno);
file.write((char *) &rec, sizeof(rec));
file.flush(); 
file.close();
}
break;
case 3: //Update Gr No.               //why is this case not working?
clrscr();                     //if the above case is not commented
cout<<"Enter the section.";    //the error is coming as case bypasses
cin>>fname;                    //initialization of a local
//variable.
file.open(fname, ios::ate | ios::in | ios::out | ios::binary);
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
int cn1=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cn1++;
if(strcmp(nm,rec.getname())==0)
{
found=1;
break;
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cn-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"Enter New city : ";
cin.getline(c,25);
file.seekp(location);
rec.updatec(nm,c);
file.write((char *) &rec, sizeof(rec));
file.flush(); 
}
file.close();
break;
}
getch();
case 5:
exit(0);
}
}
}
commented: Use code tags. +0

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:

bool proc( int val )
{
    int localVariable;
    switch (val)
    {
        case 1:
            localVariable = 1;
            break;
        default:
            if (localVariable == 1) return true;
            break;
    }
    return false;
}

What happens if 'val' is not one? The compiler warns you that localVariable may not have been initialized in the default case.

thank u so much chainsaw.

Tresa

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!