Garbage Output

OK, Everything is compiling just fine. The only problem I'm getting is the output after the math is not correct. The idea of the program is to store integers larger than what the int variable can store in a string. It is an assignment for school. Any help would be appreciated! Thanks!

Here is main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include "largeIntegers.h"
using namespace std;

int main()
{
    largeIntegers largeInt;
    cout << "Length of Integer one is : " << largeInt.length1 << " digits" << endl << endl;
    cout << "Length of Integer two is : " << largeInt.length2 << " digits" << endl << endl;
    largeInt.compareIntegers();
    largeInt.subtractIntegers();
    largeInt.multiplyIntegers();
    largeInt.addIntegers();

    system("Pause");

    return 0;
}


Here is impp.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include "largeIntegers.h"
#include <iomanip>
#include "math.h"

using namespace std;

largeIntegers::largeIntegers()
{
    readIntegers();
}

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

    cout << "Enter large integer one: ";
    cin >> bigInt1;
    cout << "Enter large integer two: ";
    cin >> bigInt2;

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

    length1 = i;
    integer = new int[i];

    k = 0;
    for (j = i - 1; j >= 0; j--)
        integer[j] = bigInt1[k++] - 48; //end processing integer one
        
    int m = 0;      //begin processing integer two
    int n = 0;
    int o;


    for (; bigInt2[m] != '\0'; m++);

    length2 = m;
    integer = new int[m];

    o = 0;
    for (n = m - 1; n >= 0; n--)
        integer[n] = bigInt2[o++] - 48;
        
}

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

void largeIntegers::outputInteger2()
{
    for (int m = length2 - 1; m >= 0; m--)
        cout << integer[m] << endl;
        
}

void largeIntegers::compareIntegers()
{
    
    convertIntegers();
     
     if (int1 == int2)
        cout << "Integers are equal." << endl;
     if (int1 < int2)
        cout << "Integer two is larger than integer one." << endl;
     if (int2 < int1)
        cout << "Integer one is larger than integer two." << endl;
}

void largeIntegers::addIntegers()
{
     convertIntegers();
       
       cout << "\n";
       
       cout << "Your integers added together is: " << setprecision(500) << int1 + int2 << endl;
       
}

void largeIntegers::subtractIntegers()
{
     convertIntegers();
     
     cout << "\n" << "The first integer subtracted from the second integer is: " << setprecision(500) << int2 - int1 << endl;
     
     cout << "\n" << "The second integer subtracted from the first integer is: " << setprecision(500) << int1 - int2 << endl;
     
}

void largeIntegers::multiplyIntegers()
{
     convertIntegers();
     
     cout << "\n" << "Your integers multiplied together is: " << setprecision(500) <<  int1 * int2 << endl;
}

double largeIntegers::convertIntegers()
{
     int1 = atoi(bigInt1.c_str());
     
     int2 = atoi(bigInt2.c_str());
     
     return int1, int2;
}


And here is .h code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef H_largeIntegers
#define H_largeIntegers
#include <iostream>
#include <string>

class largeIntegers
{
private: 

    void readIntegers(); //inputs integers
        double convertIntegers();       //convert to decimal variables to do math on

protected:
          
    double int1;
    double int2;

public:

    std::string bigInt1;
    std::string bigInt2;
    int* integer;
    int length1;
    int length2;

    largeIntegers();      // default constructor

    void outputInteger1(); // outputs integer1
    void outputInteger2(); // outputs integer2
    void addIntegers();  //add integers
    void subtractIntegers();   //subtract integers
    void multiplyIntegers();     //multiply integers
    void compareIntegers();      //compare integers
    
};
#endif
1
2
double int1;
cout << setprecision(500) << int1;

Do you know that double has rather limited precision? It is not sufficient for integral exactness.
So would I be better of with?:

1
2
3
4
long double int1;
long double int2;

cout << setprecision(500) << int1+int2;
Still, even long double doesn't have 500 digits of precision.
the precision of a double is 16 decimal digits. If you want more you will have to make your own class.
I don't really need 500 digits of precision. I was just getting a little crazy there trying to figure out why it was outputing a negative number when I add two integers of 18 digits. Does that make sense? The idea of the program is to store values in a string (values to large to fit in an integer variable) and still be able to perform math on them (that is why I convert the strings into double before i perform the math. So when I multiply two values that are over 10 digits (the max a variable of int can hold right?) I can output them in regular number notation (not scientific). Sorry if I'm confusing anyone. Any ideas on how to accomplish that goal, if this one isn't appropriate?
Old school math: 36 + 75
 36
+75
---

6+5=11, but we can keep only one digit, so we have to store the other

 1
 36
+75
---
  1

1+3+7=11. Again over the top

1
 36
+75
---
 11

1+0. Finally a direct fit

36+75=111

That was like each digit would be a separate integer. Same principle works for BigInts too. You have to recognize when things will overflow, and move the surplus to the next "part" of the BigInt.

For example, take 9 left-most digits from a string and store them into int. Take next 9 digits into second int. Next ...

Then compute piece by piece.
I was afraid of that... Any other ideas before I begin this endeavor?
Ok, I have been thinking about the problem at hand...and I have only succeeded in confusing myself... How does one go about reading the string and adding by character and splitting where it overflows? I was thinking read the string, split it into 8 digit segments(have a better idea?), convert each segment of the first integer to a int, then convert each segment of the second integer to an int, then adding the segments together, converting the added segments back to a string, then appending the strings together. Does that make sense to anyone else lol?

So if I entered: 123456789123456789 and 123456789123456789

I would split the first input to 12345678, 91234567, 89
Then split the second input to 12345678, 91234567, 89.

Then add 12345678 + 12345678 = 24691356
then add 91234567 + 91234567 = 182469134
then add 89 + 89 = 178

Then take 24691356 and append 182469134 to it, then append 178 to that. To come up with 24691356182469134178

Is that logic correct? Sorry, my brain is fried lol.

Edit: Ok, just remembered I had a calculator lol, my logic is good. So how do I split the string like that?
Last edited on
Topic archived. No new replies allowed.