程序自行终止

发布于 2025-01-09 18:52:34 字数 1521 浏览 4 评论 0原文

我正在尝试用 C++ 类实现一种名为 HugeInteger 的新数据类型。我遇到一个问题,当我尝试创建新的 HugeInteger 时,我的程序在打印第一个数字时自行终止。当我注释该语句时,语句用户输入一个 HugeInteger,但显示数字的语句仍然存在,第一个数字未写入(数字的默认值为 0),第二个数字由用户填充。我的尝试如下:

HugeInteger.h

// HugeInteger class definition


#ifndef HUGEINT_H
#define HUGEINT_H

class HugeInteger
{
private:
    int arr[40] = {0};
    size_t len;
    
    
public:
    
    void Input();
    void Output();
    
};

#endif

HugeInteger.cpp

#include "HugeInteger.h"
#include <iostream>
#include <sstream> // to fill arr


void HugeInteger::Input()
{
    std::string line;
    int i;
    std::getline(std::cin, line);
    
    for (size_t i = 0; i < line.length(); i++)
    {
        arr[i] = line[i] - '0';
    }
    len = line.length();
}

void HugeInteger::Output()
{
    int i = 0;
    while (i < len)
        std::cout << arr[i++];
}

main.cpp

#include <iostream>
#include "HugeInteger.h"

int main()
{
    HugeInteger hui = HugeInteger();
    //hui.Input(); // when this is commented, Output method does not work.
    hui.Output();
    HugeInteger hui2 = HugeInteger();
    hui2.Input();
    hui2.Output();
    return 0;
}

注意:我刚刚执行了另一个调试,我创建了另一个名为 getLen 的方法,以便我可以访问 len 属性。但是,我将 Output 方法之前和之后的代码放在 main.cpp 中,但我看不到 len 的值,也看不到这些位置。之后,我将 arr 属性设置为公共,并尝试查看它的内容,但我也看不到。我认为我的代码不起作用,在程序完成后我只能看到我输入的内容。 注意:我认为我没有面临缓冲区溢出问题,因为我总是用 3 4 位数字调试我的程序,所以我的数组包含 3-4 个数字,其余为 0。此外,我可能会放一个代码来处理这个问题。

I'm trying to implement a new datatype called HugeInteger in C++ with classes. I've encountered a problem that when I try to create new HugeInteger, my program terminates itself when the first number is printed. When I comment the statement the statement user enter a HugeInteger but the statement that displays the number is still there, the first number isn't written(the default value of the number is 0) and second number is filled by user. My try is as following:

HugeInteger.h

// HugeInteger class definition


#ifndef HUGEINT_H
#define HUGEINT_H

class HugeInteger
{
private:
    int arr[40] = {0};
    size_t len;
    
    
public:
    
    void Input();
    void Output();
    
};

#endif

HugeInteger.cpp

#include "HugeInteger.h"
#include <iostream>
#include <sstream> // to fill arr


void HugeInteger::Input()
{
    std::string line;
    int i;
    std::getline(std::cin, line);
    
    for (size_t i = 0; i < line.length(); i++)
    {
        arr[i] = line[i] - '0';
    }
    len = line.length();
}

void HugeInteger::Output()
{
    int i = 0;
    while (i < len)
        std::cout << arr[i++];
}

main.cpp

#include <iostream>
#include "HugeInteger.h"

int main()
{
    HugeInteger hui = HugeInteger();
    //hui.Input(); // when this is commented, Output method does not work.
    hui.Output();
    HugeInteger hui2 = HugeInteger();
    hui2.Input();
    hui2.Output();
    return 0;
}

Note: I've just perfomed another debug that I create another method called getLen so that I can access the len attribute. However, I put the code before and after Output method in the main.cpp, but I couldn't see the value of len neither these poisions. After that, I make the arr attribute as public, and tried to see the contents of it, and I couldn't see that either. I think my code doesn't work and I can somehow only see what I entered, after that the program is finished.
Note: I think I'm not facing a buffer overflow problem, because I always debugged my program with 3 4 digit numbers, so my array has contain 3-4 numbers and the rest is 0. Moreover, I'll probably put a code to handle this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2025-01-16 18:52:34

Input() 的调用设置 len 成员的值。如果没有调用,该成员就不会被初始化,但仍然被 Output() 使用。

只需将其初始化为 0,类似于对数组所做的操作。

The call to Input() sets the value of the len member. Without the call, the member is uninitialized, but still used by Output().

Just initialize it to 0, similar to what you do with the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文