Builds fine but wont run

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std; 
int main()
{
	
	const int months=12;
	int days[months]={31, 28, 31, 30, 
		31, 30, 31, 31, 
		30, 31, 27, 88};
	for (int count=0; count < months; count++)
	{
		cout << "month " << (count+1) << "has ";
			cout << days[months] << "days " << endl;
	}
	return 0;
}

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 const int line. int days=0 but then it wouldn't even build successfully.

Any ideas?
closed account (S6k9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std; 
int main()
{
	
	const int months=12;
	int days[months]={ 31, 28, 31, 30, 
	                   31, 30, 31, 31, 
		           30, 31, 27, 88 };

	for (int count = 0; count < months; count++){
		cout << "month " << (count+1) << " has ";
		cout << days[count] << " days " << endl;
	}

	return 0;
}


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?
Last edited on
cout << days[months] << "days " << endl;
should be
cout << days[count] << "days " << endl;

array days[] has 12 elements, subscript 0 to 11.
days[months] is accessing the 13th element, beyond the end of the array.
Last edited on
@computerquip

I'm using VC++ 2010.

@Charvil

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.
closed account (S6k9GNh0)
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 . . .



What is weird about it?

Remember the array index(or subscipt) starts from zero. So an array of 12 elements is defined like this:
int array[12];
But you cannot then do this:
cout << array[12];
as that is accessing the 13th element. The first is array[0] and the last is array[11].

I feel as though I'm repeating myself, perhaps I've not explained it very well.



Can you break it down with me?

Charvil wrote:
The first is array[0] and the last is array[11].


Isn't that what I'm saying when I say const int months=12;? Doesn't the days[months] array go from days[0] to days[11]?
Last edited on
Yes, we are agreed on that.

But the original code has this:
12
13
	cout << "month " << (count+1) << "has ";
		cout << days[months] << "days " << endl;

where the second line has this error, cout << days[months] which is effectively saying cout << days[12] and that's the cause of the problem.
Both myself and computerquip pointed out earlier that that line needs to be changed.

Oh ok so how should I fix it? should I insert a counter somewhere with a loop?
Oh ok I took a look at the book again I did make the mistake I misread it. Thanks for your help.

@mods
sorry about the blank post above I meant to delete that post but it didn't work and now I can't do anything with it.
Last edited on
Topic archived. No new replies allowed.