Just as AbstractionAnon stated, you have no idea what value a variable could be storing when you first declare it.
That test variable could be storing 0 (if you are lucky) or -242342423. You don't know.
But if you do this,
You know for sure that test now has 0 and later in the program, you can utilize the following without worrying about what initial value the variable might have
|
test += 5; // test = test + 5
|
That's why you want to initialize some variables.
Like AbstractionAnon said, I'm sure there is a way to solve this problem without utilizing the loop (As a matter of fact, your original code might even work after adjustment now that I take a second look at it). You have * instead of + between (3.0 + 0.20 * 10 ) and (numberOfPages - 10 + 0.10). And it is supposed to be (numberOfPages - 10) * 0.10, not + 0.10. When I first came up with the algorithm, I just happened to think of using the loop. You can solve the problem however you want. After all, this is your assignment.
If you are using a loop and it is not producing the result you expected, then you have to try to debug it. Get a paper and pencil and follow each iteration of the loop to find out why the program is not producing the correct result. It sounds old school, but you'd be surprised at how effective this pencil / paper approach is. Also, you can include cerr/cout statements in the program for debugging purpose and then comment them out later.
The bottom line is, your job isn't just creating the code. You have to be able to debug your own code.