Hi,
Code Tags:
http://www.cplusplus.com/articles/z13hAqkS/
First up, prefer double rather than float, the latter precision is easily exceeded. Try to avoid putting the name of types into function names, if the type changes, one has to change all the function names.
Don't loop on eof, it's too late by then :+) instead loop on the read operation:
1 2 3 4 5
|
void FloatList::getList(ifstream &List)
{
length = 0;
while (List >> values[length] && length < MAX_LENGTH) {length++;}
}
|
A for loop looks like this:
1 2
|
for (int count = 0; count < length; ++count) // the middle expression ends the loop when it becomes false
{
|
A slightly fancier and much safer way is to use a range based for loop:
1 2 3 4
|
for (const auto& item : values) { // item is a value in the array
std::cout << item << " "; // with spaces
}
std::cout << "\n";
|
This might be too advanced right now but there you go :+)
The
const
means we are not going to change the value of an item.
auto
automatically detects the type of the item. The
&
isn't really necessary here - it means that each item is a reference (another name for a variable,
kind of like a pointer), there is not a lot of point doing that for an int, so it could be left out here.
constexpr
is stronger than
const
, it's
const
'ness can't be cast away, so:
constexpr int MAX_LENGTH = 50;
But one should try to avoid global variables.
Good Luck !!