I copied this program directly from the book. It builds successfully but when I try to run it it says, "Run Time Check #3 the variable 'days' is being used without being initialized" So I thought "ok fine" I added this line just before the constint line. int days=0 but then it wouldn't even build successfully.
You either didn't copy it verbatim or the book is wrong.
Please take note of this line and how I corrected it above: cout << days[months] << " days " << endl;
EDIT: You can safely ignore the changes in whitespace. I formatted it via my editor and posted without realizing I had done it...
However, I'm surprised you claim it doesn't run. What compiler are you running?
Ok so since there're really 13 elements in the array but I only have 12 numbers wouldn't that mean that the 13th would show up as 0 or not at all? Either way, why would that cause an error? This is the reverse of the bounds checking error.It's not like I have a 12 element array with 13 numbers.
Not quite, There are really 12 elements, but your program is attempting to access the 13th, which doesn't exist. The contents of that location could be anything at all.
It seems that your compiler is actually warning you about this error.
Normally C or C++ doesn't check whether you are exceeding the array boundaries like this. It could give unexpected results or cause corruption of some other data if you were to modify such a location. Generally the result would be a program crash, but not necessarily immediately.
Actually, reading from the 13 element of that array *should be valid*. Writing is what the big no-no is. Linux will prevent you from doing this, Windows will be retarded and not care or spasm all over the place, giving undefined results. Sometimes it tells you an unexpected error occurs or it just stops the program, not telling you anything. *Sometimes it does nothing at all*. I can't stand programming on Windows...
Well that's weird. Why is it attempting to accesss the 13th element when I only have 12 numbers? Also although giving the array a 13th element fixed the error (due to a reason I don't understand) the numerical output is all messed up for some reason.
month 1 has -858993460 days
month 2 has -858993460 days
month 3 has -858993460 days
month 4 has -858993460 days
month 5 has -858993460 days
month 6 has -858993460 days
month 7 has -858993460 days
month 8 has -858993460 days
month 9 has -858993460 days
month 10 has -858993460 days
month 11 has -858993460 days
month 12 has -858993460 days
month 13 has -858993460 days
Press any key to continue . . .
Remember the array index(or subscipt) starts from zero. So an array of 12 elements is defined like this: int array;
But you cannot then do this: cout << array;
as that is accessing the 13th element. The first is array and the last is array.
I feel as though I'm repeating myself, perhaps I've not explained it very well.
where the second line has this error, cout << days[months] which is effectively saying cout << days and that's the cause of the problem.
Both myself and computerquip pointed out earlier that that line needs to be changed.