C++ Truncating Floats in Function Return -
i have written own floatarray class use project. problem i'm having when use method return value @ given index, returned value truncated int , not entire float. if use overloaded << operator made within (it accesses background array in normal manner) prints entire float fine.
any ideas i'm missing here?
my floatarray class function:
float floatarray::get(int index) const { int returnval(0); if (index > this->size-1 || index < 0) { cout << "index out of bounds!" << endl; exit(1); } else { returnval=this->array[index]; } return returnval; }
overloaded << operator:
ostream & operator<<(ostream & _ostream, floatarray &rhs) { for(int i=0; <= rhs.size-1; i++) { _ostream << rhs.array[i] << " "; } return _ostream; }
example main:
int main() { floatarray floats(2); float zero(0.12); float one(1.12); float two(2.12); floats.push(zero); floats.push(one); floats.expandby(1); floats.push(two); cout << "using overloaded << operator: " << floats << endl; cout.precision(2); cout << fixed << "using function: " << floats.get(0) << " " << floats.get(1) << " " << floats.get(2) << endl; return 0; }
main's output:
using overloaded << operator: 0.12 1.12 2.12 using function: 0.00 1.00 2.00
this problem:
v int returnval(0);
why int?
Comments
Post a Comment