how could I break this loop?

This is a discussion on how could I break this loop? within the C Programming forums, part of the General Programming Boards category; I want to break the loop after Xwin() finds a winner Code: for(;;) { Board(); // game board printf("X's turn\n"); ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    34

    how could I break this loop?

    I want to break the loop after Xwin() finds a winner

    Code:
            for(;;)
    	{
    	Board();      // game board
    	printf("X's turn\n");
    	RnC();         //   requests for user's row and column number                                
    	if(board[row][column] != 0)
    	{
    		printf("That space is already filled");
    		Sleep(700);
    		continue;
    	}
    	board[row][column] = 1;
    	Xwin();       // Checks for winning combinations of X
    	turns++;
    	if(turns > 8)
    		{
    			printf("It's a tie");
    			Sleep(1500);
    			break;
    		}
    	}

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    You mean you want to break the loop inside Xwin?
    If so, have it return a bool if the user wins, and check the return of the function and break if true.
    And you indentation is messed up, plus you mixed spaces and tabs, which is bad. Stick to one.

    Code:
    if ( Xwin() )
        break;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    You mean like this?
    Code:
    int Xwin()
    {
            for
           {
    	if(winning combinations)
    		{
    		printf("X wins!!!");
    		break;
                    return 1;
    		}
            }
    Last edited by Trafalgar Law; 09-27-2008 at 05:02 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Trafalgar Law View Post
    You mean like this?
    Code:
    int Xwin()
    {
    	if(winning combinations)
    		{
    		printf("X wins!!!");
    		break;
                    return 1;
    		}
    You want EITHER break or return. Break will jump out of [to the point just after the end of the loop] the current loop at that point, return will "jump back to where you came from" at that point.

    If you use break, you need to use a "is a win" variable that is initialized to "not winning", and if it's a win, you set it to "winning". At the end of the function, you return the "is a win" variable.

    As to which is "better" is pretty much a matter of style.

    When I recently wrote a Tic-Tac-Toe program, I made a function called "isWin", which returns: Noone, X, O or Draw - it is generic, it just tries to see if there is a row of 3 in a row in some way - or if the board is full, it will return "draw".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    34
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  2. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

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