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.