Program running, then crashing

Okay, I had an earlier post http://www.cplusplus.com/forum/beginner/125473/

I figured out what I needed to do and attempted doing it. My code is sloppy I'm sure, but it is compiling and running until the subractInteger function is called. I'm not sure what is going on. Does anyone see anything that would cause it to crash? Here is all of my code! Any help would be great!

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;
}


.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
38
#ifndef H_largeIntegers
#define H_largeIntegers
#include <iostream>
#include <string>

class largeIntegers
{
private: 

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

protected:
          
    int int1a, int1b, int1c, int1d;
    int int2a, int2b, int2c, int2d;
    double convertToIntegers();       //convert to decimal variables to do math on

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



impp 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <string>
#include "largeIntegers.h"
#include <iomanip>
#include "math.h"
#include <stdlib.h>
#include <stdio.h>
#include <sstream>

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()
{
     
     if (bigInt1 == bigInt2)
        cout << "Integers are equal." << endl;
     if (bigInt1 < bigInt2)
        cout << "Integer two is larger than integer one." << endl;
     if (bigInt2 < bigInt1)
        cout << "Integer one is larger than integer two." << endl;
}

void largeIntegers::addIntegers()
{
     convertToIntegers();
     
     int segment1 = int1a + int2a;
     int segment2 = int1b + int2b;
     int segment3 = int1c + int2c;
     int segment4 = int1d + int2d;
     
     /*string segment1str;
     string segment2str;
     string segment3str;
     string segment4str;*/
     

     stringstream ss1,ss2,ss3,ss4;
     ss1 << segment1;
     ss2 << segment2;
     ss3 << segment3;
     ss4 << segment4;
       
       cout << "\n";
       
       cout << "Your integers added together is: " << ss1 << ss2 <<  ss3 <<  ss4 << endl;
       
}

void largeIntegers::subtractIntegers()
{
     convertToIntegers();
     
     int segment1 = int1a - int2a;
     int segment2 = int1b - int2b;
     int segment3 = int1c - int2c;
     int segment4 = int1d - int2d;
     
     stringstream ss1,ss2,ss3,ss4;
     ss1 << segment1;
     ss2 << segment2;
     ss3 << segment3;
     ss4 << segment4;
     
     string segment1str, segment2str, segment3str, segment4str;
     
     segment1str = ss1.str();
     segment2str = ss2.str();
     segment3str = ss3.str();
     segment4str = ss4.str();
     
     cout << "\n" << "The first integer subtracted from the second integer is: "  << segment1str << segment2str <<  segment3str <<  segment4str << endl;
     
     //cout << "\n" << "The second integer subtracted from the first integer is: " << setprecision(30) << int1 - int2 << endl;
     
}

void largeIntegers::multiplyIntegers()
{
     convertToIntegers();
     
     int segment1 = int1a * int2a;
     int segment2 = int1b * int2b;
     int segment3 = int1c * int2c;
     int segment4 = int1d * int2d;
     
     stringstream ss1,ss2,ss3,ss4;
     ss1 << segment1;
     ss2 << segment2;
     ss3 << segment3;
     ss4 << segment4;
     
     string segment1str, segment2str, segment3str, segment4str;
     
     segment1str = ss1.str();
     segment2str = ss2.str();
     segment3str = ss3.str();
     segment4str = ss4.str();
     
     cout << "\n" << "Your integers multiplied together is: " << segment1str << segment2str <<  segment3str <<  segment4str << endl;
}

double largeIntegers::convertToIntegers()
{
//start converting string1
     string str1a = bigInt1.substr(0,7);
     int1a = atoi(str1a.c_str());
     
     string str1b = bigInt1.substr(8,15);
     int1b = atoi(str1b.c_str());
     
     string str1c = bigInt1.substr(16,23);
     int1c = atoi(str1c.c_str());
     
     string str1d = bigInt1.substr(24,31);
     int1d = atoi(str1d.c_str());
//start converting string2
     string str2a = bigInt2.substr(0,7);
     int2a = atoi(str2a.c_str());
     
     string str2b = bigInt2.substr(8,15);
     int2b = atoi(str2b.c_str());
     
     string str2c = bigInt2.substr(16,23);
     int2c = atoi(str2c.c_str());
     
     string str2d = bigInt2.substr(24,31);
     int2d = atoi(str2d.c_str());
     
     
     return int1a, int1b, int1c, int1d, int2a, int2b, int2c, int2d;
}
Try using a good debugger. If anyone else parses your code and highlights the bug, it may help you now, but in the long run you will be better off learning how to debug yourself.

Some ideas:
1. Old way: trace out your program by outputting a bunch of statements to check whether your program is performing as expected and where it is deviating.
2. Better way: Use a debugger with break-points, variables watch etc. to find the bug.
Last edited on
I know in Visual Basic you can step into your program. Is there a way to do with c++? I use devc++ (one of the versions that was picked up and is maintained). I'm not familiar with debugging in c++, aside from syntax errors. I'll see what the google can do for me :) thanks!
OK, update. I figured out what was making the program crash. It was my input (since it was less than 32 characters it crashed when it ran out of stuff to read). Any idea on how to fix that? Maybe an if statement before converting to stop reading when I have met the amount of characters it requires?
I haven't studied your code in detail, but it seems that you are doing several things wrong. In your readIntegers() function:

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
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;

//    remove this loop. I doubt if std::string is terminated with a null character.
//    for (; bigInt1[i] != '\0'; i++); 

    length1 = bigInt1.size(); // use this built-in function
    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]; // You have a memory leak here the old data in integer is lost.

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


Also, in none of your functions are you taking into account the effect of carry.

If you want only a maximum size of 32 digits for each string, you can change the readIntegers to:
1
2
3
4
5
6
do {
    cout << "Enter large integer one: ";
    cin >> bigInt1;
    cout << "Enter large integer two: ";
    cin >> bigInt2;
} while ( binInt1.size() > 32 || bigInt2.size() > 32 )
Topic archived. No new replies allowed.