Then making a const variable instead of a int would avoid something like junk data with a longer program? or would just be better in general? |
Not sure if you were talking about avoiding the magic numbers, or
const
in general.
One advantages of having a
const
variable like
MaxLines
is that if you want to change that hard coded value, you can do so in one place in the file, not here there and everywhere.
The
const
concept is a thing that not so many programming languages have, and it is a huge bonus to have it. Basically, it allows the compiler to enforce the idea that a value shouldn't change, and that is very useful.
Consider this function:
1 2 3 4 5
|
constexpr double PI = 3.14159265358979323846;
double CircleArea (const double radius) {
return radius * radius * PI;
}
|
The first thing is obvious, we don't want to be changing the value of PI. This is another usage of a
const
concept, the
const expression qualifier
constexpr
- a c++11 standard thing - one needs to compile with c++11 to use this. It is more set in stone than
const
because
const
can be cast away (removed), whereas
constexpr
is forever
const
.
constexpr
can be applied to entire expressions and even functions. The whole thing must evaluate to one value (number, string whatever) at compile time.
The second one is the parameter to the function -
radius
. By making it
const
we are saying that the value of it cannot be changed inside the function, which is natural and desirable.
Most of the time function parameters should be
const
, an exception to this is parameters passed by reference so their value is changed in the outer scope:
1 2 3 4 5 6 7 8 9
|
constexpr double PI = 3.14159265358979323846;
void CircleData (const double radius,
double& Area,
double& Circumference)
{
Area = radius * radius * PI; // the value of these variables are changed
Circumference = 2.0 * radius * PI; // in the scope which called this function
}
|
Ok, so how did you get on with my suggestion for doing 1 pass through the file?
Actually it's not that different from the snippet I gave you. The if condition should be at the start of the while loop, put
getline
inside the while loop, go from there.
I reckon you could do the whole program in 15 lines.
About pseudo code: It might seem as boring as hell - one wants to get in there a start coding flat-out right? But it is actually a very good tool for organising ones thoughts, writing down the algorithm, identifying the need for functions and loops, help in seeing where improvements can be made. The extra work is worth it - one might have a 15 line program instead of a 50 line program.
The trick is to start out very simple and general as you like. So I might have had just this:
// Print 10 lines of a file
Then, go back and add more detail and refinement until you are happy to convert it to code. Leave the comments in, if you like - they can be a form of documentation.
1 2 3
|
// const unsigned short MaxLines = 10;
// Print Maxlines lines of a file
// Print how many lines in the file
|
Then, more refinement:
1 2 3 4 5 6 7 8 9 10
|
// const unsigned short MaxLines = 10;
// Print Maxlines lines of a file
// open the file
// variable for file name std::string FileName
// Print the lines
// while loop condition <--- not sure what the condition is yet, decide later if you want
// end while loop
// close the file
// Print how many lines in the file
// variable LineCount <--- decide the type later if you want
|
So you keep going until you are happy you have enough detail.
In terms of seeing where improvements can be made, try to identify if anything is being done multiple times. This might mean a different algorithm or maybe slightly changing a function to cope with slightly different situations.
There you go, have a larrap at that :+)