Thread: program terminates when loop stops

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    program terminates when loop stops

    The following code is meant to simulate and set up a checkers board. It then asks for 2 numbers, and prints out the piece on that square. The first loop in main() runs fine, but the program ends after the loop does. Can anyone explain this please?

    Code:
    /*
    A checkers engine, for future use in programs
    
    by Alex Borland
    */
    
    #include <iostream>
    using namespace std;
    
    struct square{
           int status;
           int xPos;
           int yPos;
    };
    
    
    int main(){//The main point of interest is here...
    //--------------declare board
    square board[12][12];
    int dec1 = 0;
    int dec2 = 0;
    cout << "declaring board" << endl;
    for (dec1=0; dec1<13; dec1++){
        for (dec2=0; dec2<13; dec2++){
            board[dec1][dec2].status = 0;
        }
    }
    //the loop runs, but the program stops after the loop breaks
    cout << "board declared" << endl;
    //-------------------done declaring board, start placing pieces
    board[2][3].status = 1;//starting positions for all black pieces
    board[3][2].status = 1;//black pieces are denoted by 1, kings by 2
    board[3][4].status = 1;//an empty space has a status of 0;
    board[4][3].status = 1;//for our purposes of move restrictions, there is a 2-space border around the outside which all contain status 9 'neutral' pieces to prevent off-board movement
    board[5][2].status = 1;
    board[5][4].status = 1;
    board[6][3].status = 1;
    board[7][2].status = 1;
    board[7][4].status = 1;
    board[8][3].status = 1;
    board[9][2].status = 1;
    board[9][4].status = 1;
    
    board[2][9].status = 4;//starting positions for all red pieces
    board[2][7].status = 4;//squares occupied by a red piece are denoted by a 4, kings are 5
    board[3][8].status = 4;
    board[4][9].status = 4;
    board[4][7].status = 4;
    board[5][8].status = 4;
    board[6][9].status = 4;
    board[6][7].status = 4;
    board[7][8].status = 4;
    board[8][9].status = 4;
    board[8][7].status = 4;
    board[9][8].status = 4;
    //----------------------------------done setting up board
    cout << "board set up" << endl;
    int i = 0;
    int j = 0;
    cout << "select the X poition of the square you want to view" << endl;
    cin >> i;
    cout << "and the Y position?" << endl;
    cin >> j;
    cout << board[i][j].status;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no "loop". You need to put the code that you want to repeat into a while loop or a for loop or some other loop so that it will repeat.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    board[4][9].status = 4;
    board[4][7].status = 4;
    board[5][8].status = 4;
    // ...
    I would probably put that in a for loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    for (dec1=0; dec1<13; dec1++){
        for (dec2=0; dec2<13; dec2++){
            board[dec1][dec2].status = 0;
        }
    }
    You're going past the end of the array. Try this:
    Code:
    for (dec1=0; dec1<12; dec1++){
        for (dec2=0; dec2<12; dec2++){
            board[dec1][dec2].status = 0;
        }
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C keep a loop going whilst program continues on
    By fortune2k in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 08:44 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-08-2005, 12:51 AM
  4. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM