1

I have a char array that has number 0-8 in it in char form

Board[0] = '0';
Board[1] = '1';
Board[2] = '2';
Board[3] = '3';
Board[4] = '4';
Board[5] = '5';
Board[6] = '6';
Board[7] = '7';
Board[8] = '8';

and some of them are changed to either an 'x' or an 'o' based on user input however I need to figure out a way so that I can tell the total number of them that aren't an 'x' or an 'o'.

What I mean is if say 4 of the 9 are either an 'x' or an 'o' I need to be able to get the fact that there are 5 left. I was attempting to use for each(char c in Board) and I got far enough to where I got it to list the chars that aren't an 'x' or an 'o' but I can't figure out how to get it to send how many are left to an int value. This is as far as I got.

    for each(char c in Board)
    {
        if (c != 'x' && c != 'o')
        {

        }
    }
9
  • for each(... in ...) is not C++. Commented Dec 10, 2012 at 20:13
  • considering I wrote in in c++ and it works. yes it is Commented Dec 10, 2012 at 20:15
  • 1
    It is not standard C++ for sure. Commented Dec 10, 2012 at 20:15
  • I am using vs compiler so that may be why Commented Dec 10, 2012 at 20:16
  • 2
    Even if your compiler accepts that syntax, I wouldn't recommend submitting it. Your instructor expects you to use the syntax you've been taught this semester. Consult your textbook and class notes for the syntax of for loops. If you turn in this non-standard code, and your instructor isn't using the same compiler, then you'll probably get marked down because your instructor will be unable to test it. Commented Dec 10, 2012 at 20:20

4 Answers 4

2

You could try

auto n = std::count_if(Board, Board+9, std::isdigit);
Sign up to request clarification or add additional context in comments.

2 Comments

It is not is_digit, it is isdigit.
@Astor there seem to be no '9' ;) corrected the misspelling, thanks.
1

You should define a counter that counts the number of these characters (by incrementing it):

int n = 0;
for (char c : Board)
{
    if (c != 'x' && c != 'o')
    {
        n++; // increment n by 1
    }
}

std::cout << n << '\n'; // use the result

1 Comment

Another idea is to start with a variable counting the number of free spaces and decrement for every move.
1

You can use a combination of std::isdigit and std::count_if

#include <cctype>    // for std::isdigit
#include <algorithm> // for std::count_if

int num = std::count_if(Board, Board+9, std::isdigit);

Comments

0

Assuming you don't just want any digit, and only those between 0 and 8 you could do this:

int count = 0;

for each(char c in Board)
{
    if (c >= '0' && c <= '8')
    {
        count++;
    }
}

cout << count << " are digits between 0 and 8 (inclusive)" << endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.