I am having the strangest issue simply outputting a '-' in front of my object. My code below is a class created which adds arbitrary numbers. As you can see in main I have added the objects f and g together in a single output statement.
The '-' was successfully outputted. However, after initializing a new object h, then trying to simply output the object alone, it won't display the character. I am having no trouble with the actual workings of my program, this small bug is just starting to get very frustrating. I'm sure I overlooked something simple, but if anyone has any suggestions or solutions, that would be awesome. Thanks so much!
ps: I have also tried using a getSign member function. As you can see, this works for outputting any object as long as I implement the member function before outputting the object. Therefor, I can conclude that the constructors (implemented at the very bottom of the code) are rightfully setting the correct signs. However, I would rather avoid using this method.
int main(int argc, constchar * argv[])
{
/*InfiniteInt a("+1234567898765432123456789");
InfiniteInt b(23345155624747);
InfiniteInt c = a + b;
InfiniteInt d(c);
InfiniteInt e = d + c;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << e + d << endl;*/
InfiniteInt f("-12345699680948593458");
InfiniteInt g(-123234532);
InfiniteInt h(-123);
cout << f + g << endl;
cout << f.getSign() << f << endl;
cout << g.getSign() << g << endl;
cout << h << endl;
return 0;
}
// Overloaded << Operator
ostream &operator<<(ostream &out, const InfiniteInt rhs){
//Check for sign in display
if (rhs.sign == '-'){
out << '-';
}
//Output the newly added integer
for (int i = 0; i < rhs.brokenNumber.size(); i++) {
out << rhs.brokenNumber.at(i);
}
return out;
}
// //
//All Class-specific constructors defined here//
// //
// Default Constructor
InfiniteInt::InfiniteInt(){
brokenNumber.clear();
sign = '+';
return;
}
// Overloaded constructer that extracts single numerical characters and stores them
// inside brokenNumber vectors
InfiniteInt::InfiniteInt(const string &numberToAdd){
bool ifSign = false; //checks if user input a negative or possitive sign
char temp = '0'; //char extraced from string
int pushback = 0; //in extracted from char
if (numberToAdd[0] == '-') {
sign = '-';
ifSign = true;
}
else{
sign = '+';
}
stringstream ss;
ss << numberToAdd;
//convert string into int, for pushback
ss >> pushback;
ss.str(""); //clear the stringstream
ss.clear(); //clear error flags
//element counter
int i = 0;
//if first element is a sign char, skip
if (ifSign) {
i = 1;
}
// used to convert char to int
stringstream str;
while (numberToAdd[i]) {
temp = numberToAdd[i];
str << temp;
str >> pushback;
str.clear();
brokenNumber.push_back(pushback);
i++;
}
}
// Constructor, takes in a long long
InfiniteInt::InfiniteInt(constlonglong &newInt){
bool ifNeg = false;
longlong tempInt = newInt;
int pushback = 0;
//check if number is negative
if (newInt < 0){
ifNeg = true;
sign = '-';
tempInt = newInt * -1;
}
else{
sign = '+';
}
// Used to extract single ints and pushes them back into vector
while (tempInt > 0) {
pushback = tempInt % 10;
brokenNumber.push_back(pushback);
tempInt = tempInt / 10;
}
reverse(brokenNumber.begin(), brokenNumber.end());
}
// Constructor, which takes in an infinite int object
InfiniteInt::InfiniteInt(const InfiniteInt &obj){
brokenNumber = obj.brokenNumber;
}