Infinite loop?
Hey, I'm trying to write some code to basically have subtraction allowed for a class, and I'm stuck in a loop somewhere and I don't know where it is. I'm not too good with the debugger, too, bleh. I'll bold the part I had to make/made and if anyone can tell where I get stuck, thanks! I'm including iostream and string. The part I'm working with and where the infinite loop is between a START HERE and END HERE in the code.
EDIT: before I post it where it's jumbled what's the thing I put before and after the code? Isn't it like <*pre*> or something? Someone must know, methinks.
Thanks, here it is.
EDIT: before I post it where it's jumbled what's the thing I put before and after the code? Isn't it like <*pre*> or something? Someone must know, methinks.
Thanks, here it is.
#include
#include
using namespace std;
class INT {
int digits[100];
char sign;
public:
INT( ) {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT(int num) {
int a;
for (a=0; a<100; a++) digits[a] = 0;
if (num < 0) { sign = 'n'; num = num * -1; }
else sign = 'p';
a = 99;
while (num > 0) {
digits[a] = num % 10;
num = num / 10;
a = a - 1;
}
}
friend INT operator+(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
!!!!START HERE!!!!! else {
int a = 0, c = 1;
INT xx = x, yy = y;
while (a < 100){
if (x.digits[a] == y.digits[a])
a++;
else if (x.digits[a] > y.digits[a])
result.sign = x.sign;
else if (y.digits[a] > x.digits[a])
result.sign = y.sign;
}
for (int b = 99; b >= 0; b--) {
c = 100 - b;
if (xx.digits[b] > yy.digits[b])
result.digits[b] = xx.digits[b] - yy.digits[b];
else if (xx.digits[b] == yy.digits[b])
result.digits[b] = 0;
else if (xx.digits[b] < yy.digits[b]) {
while ((xx.digits[b-c] != 0) || (c < 100))
c++;
if (c == 100)
result.digits[b] = yy.digits[b];
xx.digits[b] = xx.digits[b] + 10;
xx.digits[b-c] = xx.digits[b-c] - 1;
result.digits[b] = xx.digits[b] - yy.digits[b];
}
}
}!!!!!END HERE!!!!!
return result;
}
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a[Error: Irreparable invalid markup ('<number.length();>') in entry. Owner must fix manually. Raw contents below.]
Hey, I'm trying to write some code to basically have subtraction allowed for a class, and I'm stuck in a loop somewhere and I don't know where it is. I'm not too good with the debugger, too, bleh. I'll bold the part I had to make/made and if anyone can tell where I get stuck, thanks! I'm including iostream and string. The part I'm working with and where the infinite loop is between a START HERE and END HERE in the code.
EDIT: before I post it where it's jumbled what's the thing I put before and after the code? Isn't it like <*pre*> or something? Someone must know, methinks.
Thanks, here it is.
<lj-cut text="Code"> <code>
#include <iostream>
#include <string>
using namespace std;
class INT {
int digits[100];
char sign;
public:
INT( ) {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT(int num) {
int a;
for (a=0; a<100; a++) digits[a] = 0;
if (num < 0) { sign = 'n'; num = num * -1; }
else sign = 'p';
a = 99;
while (num > 0) {
digits[a] = num % 10;
num = num / 10;
a = a - 1;
}
}
friend INT operator+(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
!!!!START HERE!!!!! else {
int a = 0, c = 1;
INT xx = x, yy = y;
while (a < 100){
if (x.digits[a] == y.digits[a])
a++;
else if (x.digits[a] > y.digits[a])
result.sign = x.sign;
else if (y.digits[a] > x.digits[a])
result.sign = y.sign;
}
for (int b = 99; b >= 0; b--) {
c = 100 - b;
if (xx.digits[b] > yy.digits[b])
result.digits[b] = xx.digits[b] - yy.digits[b];
else if (xx.digits[b] == yy.digits[b])
result.digits[b] = 0;
else if (xx.digits[b] < yy.digits[b]) {
while ((xx.digits[b-c] != 0) || (c < 100))
c++;
if (c == 100)
result.digits[b] = yy.digits[b];
xx.digits[b] = xx.digits[b] + 10;
xx.digits[b-c] = xx.digits[b-c] - 1;
result.digits[b] = xx.digits[b] - yy.digits[b];
}
}
}!!!!!END HERE!!!!!
return result;
}
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
int main() {
INT a, b, c;
cout << "Enter two large integers: ";
cin >> a >> b;
c = a + b;
cout << "The result is: " << c << endl;
return 0;
}
</code> </lj-cut>
For some reason that still didn't work right and i apologize for how it looks.
EDIT: before I post it where it's jumbled what's the thing I put before and after the code? Isn't it like <*pre*> or something? Someone must know, methinks.
Thanks, here it is.
<lj-cut text="Code"> <code>
#include <iostream>
#include <string>
using namespace std;
class INT {
int digits[100];
char sign;
public:
INT( ) {
sign = 'p';
for (int a=0; a<100; a++)
digits[a] = 0;
}
INT(int num) {
int a;
for (a=0; a<100; a++) digits[a] = 0;
if (num < 0) { sign = 'n'; num = num * -1; }
else sign = 'p';
a = 99;
while (num > 0) {
digits[a] = num % 10;
num = num / 10;
a = a - 1;
}
}
friend INT operator+(const INT&, const INT&);
friend ostream& operator<<(ostream&, const INT&);
friend istream& operator>>(istream&, INT&);
};
INT operator+(const INT& x, const INT& y) {
INT result;
if (x.sign == y.sign) {
result.sign = x.sign;
int a, carry = 0, total;
for (a=99; a>=0; a--) {
total=x.digits[a]+y.digits[a]+carry;
if (total > 9) carry = 1;
else carry = 0;
result.digits[a] = total % 10;
}
}
!!!!START HERE!!!!! else {
int a = 0, c = 1;
INT xx = x, yy = y;
while (a < 100){
if (x.digits[a] == y.digits[a])
a++;
else if (x.digits[a] > y.digits[a])
result.sign = x.sign;
else if (y.digits[a] > x.digits[a])
result.sign = y.sign;
}
for (int b = 99; b >= 0; b--) {
c = 100 - b;
if (xx.digits[b] > yy.digits[b])
result.digits[b] = xx.digits[b] - yy.digits[b];
else if (xx.digits[b] == yy.digits[b])
result.digits[b] = 0;
else if (xx.digits[b] < yy.digits[b]) {
while ((xx.digits[b-c] != 0) || (c < 100))
c++;
if (c == 100)
result.digits[b] = yy.digits[b];
xx.digits[b] = xx.digits[b] + 10;
xx.digits[b-c] = xx.digits[b-c] - 1;
result.digits[b] = xx.digits[b] - yy.digits[b];
}
}
}!!!!!END HERE!!!!!
return result;
}
ostream& operator<< (ostream& os, const INT& num) {
int a = 0;
if (num.sign == 'n') os << '-';
while (a < 99 && num.digits[a] == 0)
a++;
while (a < 100)
os << num.digits[a++];
return os;
}
istream& operator>> (istream& is, INT& num) {
string number;
is >> number;
if (number.at(0) == '-') num.sign='n';
if (number.at(0) == '+') num.sign='p';
int start = 0;
if ( !isdigit(number.at(0)) ) start = 1;
int loc = 100-(number.length()-start);
for (int a=start; a<number.length(); a++)
num.digits[loc++] = number.at(a) - '0';
return is;
}
int main() {
INT a, b, c;
cout << "Enter two large integers: ";
cin >> a >> b;
c = a + b;
cout << "The result is: " << c << endl;
return 0;
}
</code> </lj-cut>
For some reason that still didn't work right and i apologize for how it looks.
