c++ - Returning garbage data from a classes private array -
i have class assignment have write program take number user no more 50 digits long. have class called number
has array called value[50]
used holding value of number. number length, use of arrays, requirement of assignment.
the declaration of number
class follows:
class number { private: int value[50]; int numbersize; public: number(); number(int&, int&); int getvalue(); number& operator=(const number&); };
and definition
number :: number(int& numbervalue, int& newnumbersize) { numbersize = newnumbersize; (int count = newnumbersize; count > 0; count --) { value[newnumbersize] = numbervalue % 10; numbervalue = numbervalue / 10; cout << value[numbersize] << endl; } } int number :: getvalue() { (int count2 = 0; count2 < numbersize; count2 ++) { cout << value[count2] << endl; } return 0; }
i've omitted superfluous information such #include
directives. problem function number :: getvalue()
, written in it's current form testing purposes. when run program
int main() { number blah = number(12345, 5); }
(again, suitable simplification of real thing), following output
5 4 3 2 1 2347696 2342900 2347268 2352860 -1082078616
the numbers 5
1
@ start expected cout
found @ , of number :: number(int& numbervalue, int& newnumbersize)
constructor, , latter half coming the cout
in number :: getvalue()
, although values unexpected. expecting same values first output stream, in reverse. explicit values not reproducible, although format (four 7 digit positive integers , ten digit negative integer). please note, cout
statements in class functions there testing purposes ;)
can see i'm going wrong here?
you're not setting values.
value[newnumbersize] = numbervalue % 10
that sets same element (the last element) new value, , uses same value cout
result it's calculating it. count
isn't used inside loop. additionally, want set values value[newnumbersize-1]
value[0]
, not value[newnumbersize]
value[1]
. (the constructor loop break before count reaches zero)
passing simple values reference when not modifying them not practice.
also, getvalue()
not reach element zero, although isn't contributing incorrect output yet.
Comments
Post a Comment