1

I am writing a program that asks the user to type in a very large int (much larger than the type int can handle). When receive this int from the user, it is stored in a string. Then, I want to convert this string into an int array (I am using a dynamic int array). After compiling and running the program, I get values that don't make sense. The values of my int array seem to be random gibberish. I don't see why this is so - it doesn't look like my loops are out of bound in the converting process. Please help. The purpose of creating an int array is to then come up with ways to add, subtract, multiply, and compare very large int values. To make it clear what I am intending to do: say the user types in "12345". I want to store this string value into an int array that would have a length of 5, each element corresponding to the next number in the int.

largeIntegers.h

#ifndef H_largeIntegers
#define H_largeIntegers
#include <iostream>
#include <string>

class largeIntegers
{
private: 

    void readInteger();
    // reads integer

public:

    std::string s_integer;
    int* integer;
    int length;

    largeIntegers();
    // default constructor

    void outputInteger();
    // outputs integer
};
#endif

largeIntegers.cpp

#include <iostream>
#include <string>
#include "largeIntegers.h"
using namespace std;

largeIntegers::largeIntegers()
{
    readInteger();
}

void largeIntegers::readInteger()
{
    int i = 0,j = 0, k;

    cout << "Enter large integer: ";
    cin >> s_integer;

    for (; s_integer[i] != '\0'; i++);

    length = i;
    int* integer = new int[i];

    k = 0;
    for (j = i - 1; j >= 0; j--)
        integer[j] = s_integer[k++] - 48;
}

void largeIntegers::outputInteger()
{
    for (int i = length - 1; i >= 0; i--)
        cout << integer[i];
}

User.cpp

#include <iostream>
#include <string>
#include "largeIntegers.h"
using namespace std;

int main()
{
    largeIntegers a;
    cout << a.length << endl << endl;
    cout << a.integer[0] << endl << a.integer[1] << endl;
    a.outputInteger();
    cout << endl << endl;

    return 0;
}

I intentionally made the variables in the header public for debugging purposes. My output on the console after compiling is:

Enter large integer: 111

3

952402760

1096565083

10966961571096565083952402760

2
  • 2
    Larger than even long long int? Then you might want to check libraries such as GMP. Commented Nov 28, 2013 at 8:52
  • @JoachimPileborg I forgot to mention this is a homework assignment of mine in my first C++ course. The size of the int would be greater than the max length that C++ provides. The purpose of this assignment is to use dynamic int arrays in order to carry out addition, subtraction, etc. on large integer values. Commented Nov 28, 2013 at 9:03

2 Answers 2

4

This is the problem

int* integer = new int[i];

change to

integer = new int[i];

Your version declares a local variable that just happens to have the same name as your class variable. Easy mistake to make.

Sign up to request clarification or add additional context in comments.

1 Comment

Someone ought to say the obvious: better yet... use a std::vector<int> (or of char - more than large enough to hold 0-9 digit values).
0

also, using standards facilities like std::vector and std::getline would make your code much cleaner in addition to avoid the problem you had, and resolve memory leaks you have now if you call readInterger twice:

void largeIntegers::readInteger()
{
  cout << "Enter large integer: ";
  std::getline(std::cin, s_integer);
  integer = std::vector(s_integer.size());

  //your last loop to fill the array probably can be replaced by std::transform

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.