Thread: How can I end a function if found to be inValid?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    61

    How can I end a function if found to be inValid?

    I am writing this program and one of the functions is ifValid().
    So ifValid is supposed to tell me whether there are too many X's or O's on a tic tac toe board. When i run the program it works, except for then it continues into the next function and then adds those print lines too.

    How can I end this function if ifValid finds that the board is invalid?

    Here is the code for this function:
    Code:
    bool ifValid(int topL, int topC, int topR, int cenL, int cenC,             int cenR, int botL, int botC, int botR)
    {
       bool success = false;
       int total = 0;
       if (topL == 'X')
           total++;
       if (topC == 'X')
          total++;
       if (topR == 'X')
          total++;
       if (cenL == 'X')
          total++;
       if (cenC == 'X')
          total++;
       if (cenR == 'X')
          total++;
       if (botL == 'X')
          total++;
       if (botC == 'X')
          total++;
       if (botR == 'X')
          total++;
    
    
       if (total > 5)
          { 
          printf("Invalid Board.  There are too many X's.\n"); 
             
          }   
             
       else if(total <= 3)
          {
          printf("Invalid Board.  There are too many O's.\n");
          
          }
       (!success);
       return success; 
        }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Just stick a "return" right after the printf statements.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Member function not found?
    By SterlingM in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2011, 10:50 AM
  2. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  3. OverLoaded Member Function Not Found
    By lifeafterdeath in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 10:43 AM
  4. Pq - Sq - Kq --- Found!!!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-02-2002, 07:07 PM
  5. mysterious invalid function errors
    By frenchfry164 in forum C++ Programming
    Replies: 14
    Last Post: 07-29-2002, 09:58 PM