如何从C ++中的以下情况中删除异常错误 [英] How do I remove the exception error from the following case in C++

查看:90
本文介绍了如何从C ++中的以下情况中删除异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个特殊情况在我的c ++程序中显示异常13错误...我该怎么纠正它...请帮助



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;





我的尝试:



i尝试重写这段代码,从老师给我的项目中检查了两次但没有出现



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

推荐答案

你应该学会使用调试器尽快地。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



建议:

- 使用调试器查看异常发生的位置。

- 避免在代码中使用。

Nota:

- 通过不给出变量的类型和大小,你确保我们无法弄清楚什么是代码中出错。
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.


如前所述,您需要学习如何使用调试器。此外你绝对应该避免使用 goto ,发布的代码实在是一团糟。
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.


我必须猜测,因为你没有告诉我们确切的错误消息。但例外13可能是一般保护错误 - 维基百科,免费百科全书 [ ^ ]。



最常见的原因对于这些异常是内存访问错误(访问冲突)。因此,如果您在某处传递无效指针或访问数组超出范围(例如,写入超出数组的大小),请检查您的代码。



我(和其他人在这里)不能这样做,因为你没有显示你的变量的声明(我们不知道你的字符串缓冲区的大小和 v 类的类型) 。



请注意,最后一点(您的 v 类)可能是因为您正在读取数据从文件。如果类包含非整数成员(例如指向对象,已分配的内存或任何其他类似行为的类),则无效。



我知道你是初学者,但即使初学者代码也很难看:



  • 你正在混合使用C和C ++ IO方法(getXXX函数和流)
  • 您正在使用 goto
  • 您在开关中有大量代码 block
  • 您没有检查打开文件是否成功
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


这篇关于如何从C ++中的以下情况中删除异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆