Another easy4u loop question

This is a discussion on Another easy4u loop question within the C++ Programming forums, part of the General Programming Boards category; Well, I've finally figured a few things out on my own before coming to you guys, but here I am ...

  1. #1
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Angry Another easy4u loop question

    Well, I've finally figured a few things out on my own before coming to you guys, but here I am again!
    This code down here seems as straight-forward as I can think of, so why does it crash? I've tried everything. I don't think the loop ever stops.
    I'm just attempting to take information out of one array and putting it into another. My counters work ok ( I think). Can you believe that I've been working on making this for about a week! Today I finally got it *yippie* then it crashes lol.

    I've included only the part that crashes, the rest works fine:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
    	const int MAX=12;
    	const int iMAX=31;
    	long months[iMAX] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
    	int hold[MAX];
    	int i=0;
    	hold[i] = months[i];
    
    	while(i < MAX)
    	{
    	cout << i+1 << ": " << months[i]
    		 << "       " 
    		 << "Hold= " << hold[i]  << "\n";
    	i++;
    	hold[i] = months[i];
    	}
    	cout << "\n\nNotice how this program crashes! Why?\n\n";
    	system("pause");
    	return 0;
    }
    -----
    Also, now learning pointers, they are awful dreaded gross things, my book seems to jump from simple to "masterful" in about 2 pages, can anyone recommend a good book?

    Thanks again people,

    Rob Sitter

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Notice that you increment i near the end of the loop, but then copy the elements from months to hold after the increment. The last time through the loop, i will be one less than MAX, then you increment it to be MAX, and then you attempt to assign the element at index MAX in months to the space at index MAX in hold. Array indexes are 0 based, so you are accessing the array passed its bounds leading to a crash.

    You should do the increment after you make the assignment .

  3. #3
    Master At Novice
    Join Date
    Oct 2005
    Location
    Cardassia & Canada, eh!
    Posts
    31

    Last post to close this thread>> Told you it was easy4u lol :)

    Quote Originally Posted by Daved
    Notice that you increment i near the end of the loop, but then copy the elements from months to hold after the increment. The last time through the loop, i will be one less than MAX, then you increment it to be MAX, and then you attempt to assign the element at index MAX in months to the space at index MAX in hold. Array indexes are 0 based, so you are accessing the array passed its bounds leading to a crash.

    You should do the increment after you make the assignment .
    Ok, thanks!

    Rob Sitter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 09:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-30-2008, 12:12 AM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 12:43 AM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 01:08 PM
  5. A question about while loop
    By Arooj in forum C++ Programming
    Replies: 5
    Last Post: 02-16-2003, 10:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21