开发者

Returning garbage data from a classes private array

I have a class assignment where I have to write a program that will take a number from the user that is no more than 50 digits long. I have a class called Number that has an array called Value[50] used for holding the value of the number. The number length, as well as the use of arrays, is a requirement of the assignment.

The declaration of the Number class is as follows:

class Number
{
private:
  开发者_如何学C  int Value[50];
    int NumberSize;
public:
    Number();
    Number(int&, int&);
    int GetValue();
    Number& operator=(const Number&);
};

and the definition is

Number :: Number(int& NumberValue, int& NewNumberSize)
{
    NumberSize = NewNumberSize;
    for (int count = NewNumberSize; count > 0; count --)
    {
        Value[NewNumberSize] = NumberValue % 10;
        NumberValue = NumberValue / 10;
        cout << Value[NumberSize] << endl;
    }
}

int Number :: GetValue()
{
    for (int count2 = 0; count2 < NumberSize; count2 ++)
    {
        cout << Value[count2] << endl;
    }
    return 0;
}

I've omitted superfluous information such as #include directives. My problem is with the function Number :: GetValue(), which is written in it's current form for testing purposes. When I run the program

int main()
{
    Number blah = Number(12345, 5);
}

(again, a suitable simplification of the real thing), I get the following output

5
4
3
2
1
2347696
2342900
2347268
2352860
-1082078616

The numbers 5 to 1 at the start are expected from the cout found at the and of the Number :: Number(int& NumberValue, int& NewNumberSize) constructor, and the latter half is coming from the the cout in Number :: GetValue(), although the values are unexpected. I was expecting the same values as from the first output stream, only in reverse. The explicit values are not reproducible, although the format is (four seven digit positive integers and a ten digit negative integer). Please note, the cout statements in the class functions are only there for testing purposes ;)

Can anyone see where I'm going wrong here?


You're not actually setting your values.

Value[NewNumberSize] = NumberValue % 10

That just sets the same element (the last element) to the new value, and uses that same value to cout the result as it's calculating it. count isn't used inside the loop. Additionally, You probably want to set values Value[NewNumberSize-1] to Value[0], not Value[NewNumberSize] to Value[1]. (The constructor loop will break before count reaches zero)

Passing simple values by reference when not modifying them is not good practice.

Also, GetValue() does not reach element zero, although that isn't contributing to incorrect output yet.


Value[NewNumberSize] = NumberValue % 10;

This should be

Value[count] = NumberValue % 10;

Also, you need to fix the range of count so that the array index starts from 0 to size-1, rather than 1 to size.

Finally, you are changing the value of NumberValue in the constructor, while the variable is passed by the reference. It's not a good practice. Do not your call-by-reference here or do not change the value of NumberValue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜