Strange Output Error, probably something simple

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!

Output looks like the following:

-12345699681071827990
-12345699680948593458
-123234532
123


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.

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
int main(int argc, const char * 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(const long long &newInt){
    
    bool ifNeg = false;
    long long 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;
}
Last edited on
On line 31 you pass by copy instead of by reference. Since the sign is not being detected properly this means your copy constructor is also wrong.
Wow, just changing line 31 to a pass by reference did the trick! I figured it was something simple, just couldn't locate the issue for some reason.

Thanks so much for the help!
Topic archived. No new replies allowed.