为什么我的C ++代码崩溃了 [英] Why my C++ code is crashing

查看:76
本文介绍了为什么我的C ++代码崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码崩溃



我已经使用过调试器但无法猜测他们发生了什么错误



我尝试过:



my code is crashing

I already used Debugger But can't guess what them mistake happening

What I have tried:

#include<iostream>
#include <string>
using namespace std;
int test;
int main()
{
 cin >> test;
 string name;
 string three;

for(int i=0;i<test;i++)
{getline(cin,name);

int j=1;
for(int k=0;k<name.length()-2;k++)
{
    three=name.substr(k,3);//IT include i i+1 i+2
    if(three=="101" || three=="010")
      {
        break;
      }
     else
        ++j;
 }
 if (j<name.length()-2)
 {
    cout << "good" << endl;
 }
 else
    cout << "bad" << endl;
 }
}

推荐答案

使用调试器。

停止猜测并查找证据。

在函数开头放置一个断点,在调试器中运行你的应用程序,然后依次逐步执行每一行。使用调试器查看变量中的内容。

在执行每一行之前,先弄清楚你预期会发生什么,然后执行该行,看看发生了什么。这是你的期望吗?如果是这样,继续前进。如果不是......为什么不呢?有什么不同?发生了什么事,你没想到,或者没有发生过你的事情?



调试是一项技能:你只能通过使用它来开发它。在这样一个简单的代码示例上开发它比在100,000行完整项目上开发它要好得多!

试一试:看看你能找到什么信息!
Use the debugger.
Stop "guessing" and look for evidence.
Put a breakpoint at the start of the function, run your app in the debugger, and step through each line in turn. Use the debugger to look at what is in what variable.
Before you execute each line, work out what you expect to happen first, then execute the line and see what did happen. Was it what you expected? If so, move on. If not ... why not? What was different? What happened that you didn't expect, or didn't happen that you did?

Debugging is a skill: you only develop it by using it. And it's a lot better to develop it on a trivial code sample like this than on a 100,000 line complete project!
Give it a try: see what information you can find out!


这篇关于为什么我的C ++代码崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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