1

Hi I am trying to program a simple tic tac toe game, and whenever I compile it says:

error: ISO C== forbids comparison between pointer and integer
[-fpermissive]  } while(input != "Quit");
                                 ^

Here is my code:

#include <iostream>
using namespace std;

/*
Board;
 1 | 2 | 3
---|---|---
 4 | 5 | 6
---|---|---
 7 | 8 | 9
*/

char square[9] = {'1','2','3','4','5','6','7','8','9'};

char input;

void board();

main()
{
    do
    {
        board();
        switch (input)          
        {
            case 1:
                square[1] = 'X';
            case 2:
                square[2] = 'X';
            case 3:
                square[3] = 'X';
            case 4:
                square[4] = 'X';
            case 5:
                square[5] = 'X';
            case 6:
                square[6] = 'X';
            case 7:
                square[7] = 'X';
            case 8:
                square[8] = 'X';
            case 9:
                square[9] = 'X';
            default:
                cout << "Invalid Input";
        }
    } while(input != "Quit");     //Here is where it is an error

    if(input == "Quit")
    {
        return 1;
    }

    cout << "\n\n"; 

    return 0;
}

void board()                //Builds the board
{
    cout << "\n\n\tTicTacToe\n\n";

    cout<<" "<<square[0]<<" | "<<square[1]<<" | "<<square[2]<< endl;
    cout << "---|---|---" << endl;

    cout<<" "<<square[3]<<" | "<<square[4]<<" | "<<square[5]<< endl;
    cout << "---|---|---" << endl;

    cout<<" "<<square[6]<<" | "<<square[7]<<" | "<<square[8]<< endl;

    cout << "Player 1 Make a Move:  ";
    cin.get();
    cin >> input;
}

FYI, this isn't close to the full game, I am just trying to figure out how I will code some parts of the game.

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • `input` is of type `char` while `"Quit"` is of type `const char *`. You can't compare a character with a string (an array of characters). Maybe you are looking for `while(input == 'Q')` – simpel01 Nov 21 '15 at 17:56
  • How do I make "Quit" type const char* ? – Harrison Rankin Nov 21 '15 at 17:58
  • "Quit" is already of type `const char*`, but you need a `char` to be able to compare with `input`, Try using `while(input == 'Q')` – simpel01 Nov 21 '15 at 18:00
  • 1
    why does the question end with the words **strong text**? – jochen Nov 21 '15 at 18:14
  • You are comparing two different types of values. char and char * – Flare Cat Nov 21 '15 at 18:50
  • 1
    Also, consider using std::string – Flare Cat Nov 21 '15 at 18:52
  • You can replace your `switch` statement with: `if ((input >= 1) && (input <= 9)) square[input] = 'X';`. By the way, the first cell in an array is 0, as in `square[0]`. If you want to use indices 1 ... 9, then declare your array with 10 elements (so you are ignoring the element at index 0). – Thomas Matthews Nov 21 '15 at 20:02
  • Possible duplicate of [c++ compile error: ISO C++ forbids comparison between pointer and integer](http://stackoverflow.com/questions/2263681/c-compile-error-iso-c-forbids-comparison-between-pointer-and-integer) – Makyen Mar 02 '17 at 22:50

5 Answers5

0

The problem is that variable input is declared as a single char data type and you're comparing it with a string, or in this case a const char*, essentially an array of characters. Since "Quit" is an array of characters, you cannot equate that to a SINGLE char.

m_callens
  • 6,100
  • 8
  • 32
  • 54
0

When you try to assign or compare two variables the left operator type must be the same that le right operator type, in your casse le left operator is input, a variable of type char and the left variable type "Quit" is of type const char[4], there two types are different and can't be compare !

panic
  • 2,004
  • 2
  • 17
  • 33
0

In the if, you are comparing a char to char *.

Either set the value in the if to

'Q'

Or declare the input variable using

char *input;

Consider looking at the string header, it includes features that are highly useful in this situation.

Edit: Another error with your code: In your switch case, you do not include a break; after each case :. This will cause every everything after the correct case to become 'X'.

Edit 2: You should NOT use global variables. Just pass them by reference.

Edit 3: main() should have a type.

Flare Cat
  • 591
  • 2
  • 12
  • 24
0

In your switch statement, you are accessing square[9] which doesn't exist. This is called a buffer overrun and you may be overwriting other variable or code.

Review with your board() function, which accesses the array correctly.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • This is true; his indexing to start at 0. But, the issue lies in the fact that he is comparing to types of values that cannot be compared, `char *` and `char`. – Flare Cat Nov 22 '15 at 14:19
0

The first one is char and can't be compared with char* which is a char pointer.
And please use std::string (or string in this case) that's an object instead of char* which is a primitive type.

It will be something like:

string input;
Plantt
  • 230
  • 1
  • 10