Click here to Skip to main content
14,691,272 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this particular case in my c++ program shows a exception 13 error...how can i correct it...please help

case 4:
//here house wise
clrscr();
f1=0;
check=0;
V21:
cout<<"Enter house whose results are to be displayed:"<<endl;
gets(input1);
flag1=validstring(input1);
if(flag1==0)
goto V21;
else
strcpy(house1,input1);
ifstream ifile8("votingsystem",ios::binary);
while(!ifile8.eof())
{
  check++;
  ifile8.read((char*)&v,sizeof(v));
  if(ifile8.eof())
  {
    check++;
    break;
  }
  if((strcmpi(house1,v.gethouse()))==0)
  {
    if(f1==0)
    {
      cout<<"........................................................................\n";
      cout<<setw(20)<<"NAME OF THE CANDIDATE"<<setw(10)<<"CLASS"<<setw(10)<<"AGE"<<setw(10)<<"GENDER"<<setw(10)<<"HOUSE"<<setw(10)<<"VOTES"<<"\n";
      cout<<"............................................................................................................................................\n";
    }
    v.displaytabresult();
    f1++;
  }
}
ifile8.close();
if(check==2)
{
  cout<<"Please enter details first/n";
  getch();
  goto V1;
}<a href=""></a>
if(f1==0)
cout<<"No such house exists"<<endl;
getch();
goto V6;


What I have tried:

i tried rewriting this code, checked it twice from the project given to me by the teacher but nothing came up
Posted
Updated 24-Aug-16 22:19pm
v2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advices:
- Use the debugger to see where the exception occurs.
- avoid gotos in code.
Nota:
- by not giving type and size of variables, you make sure that we can't figure out what is wrong in the code.
   
v2
Comments
CPallini 25-Aug-16 2:58am
   
5.
Patrice T 25-Aug-16 3:14am
   
Thank you
As already suggested you need to learn how to use the debugger. Moreover you should definitely avoid using goto, the posted code is really a mess.
   
I have to guess because you did not show us the exact error message. But "exception 13" may be a General protection fault - Wikipedia, the free encyclopedia[^].

The most common reason for these exception are memory access errors (access violations). So check your code if you pass an invalid pointer somewhere or access an array out of bounds (e.g. writing beyond the size of the array).

I (and others here) can't do that beacuse you did not show the declarations for your variables (we did not know for example the sizes of your string buffers and the type of your v class).

Note that the last point (your v class) may be the reason because you are reading the data from file. This won't work if the class contains non-integral members (e.g. pointers to objects, allocated memory, or any other classes that behave similar).

I know that you are a beginner but even for a beginner the code is ugly:

  • You are mixing C and C++ IO methods (getXXX functions and streams)
  • You are using goto
  • You have large blocks of code inside a switch block
  • You did not check if opening the file was successful
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900