Home > Back-end >  c wont take absolute value of a number
c wont take absolute value of a number

Time:04-10

I create a tic tac toe AI in c . I've tried adding code to add X to where the player put it, but when I run it gives me an error related to line 37. There's an expression that simply converts a set of coordinates into a number. Rearranging the code gives me another error. The error might be related to how I ask for the absolute value.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<string>> Board
    {
        {" ", " ", " "},
        {" ", " ", " "},
        {" ", " ", " "}
    };
    string playerInput;
    
    bool hasMoved = false;
    
    bool run = true;
    while(run) {
        //Prints Tic Tac Toe board to the screen
        for(int h = 0; h < Board.size();h  ){
            cout << " ";
            for(int d = 0; d < Board[h].size();d  ){
                cout << Board[h][d];
                if(d < Board[h].size() - 1){ cout <<"||"; }
            }
            if(h < Board.size() - 1){ cout << "\n---------\n"; }
            
        }
        cout << "\n choose a number 1-9 to place an X: ";
        cin >> playerInput;
        
        hasMoved = false;
        while(!hasMoved){
            for(int h = 0; h < Board.size();h  ){
                for(int d = 0; d < Board[h].size();d  ){
                    if(Board[h].size() * abs(h - Board[h].size()   1)   d == playerInput && Board[h][d] == " ") {
                        Board[h][d] = "X";
                        hasMoved = true;
                    }
                }
            }
            if(!hasMoved){
                cout << "\n choose a number 1-9 to place an X: ";
                cin >> playerInput;
            }
        }
    }
}

CodePudding user response:

This does not work the way you expect:

abs(h - Board[h].size()   1)

The call to vector::size() returns a size_t value, which you are subtracting from a int value. By the rules of arithmetic conversions the int will be converted to unsigned.

So what you have is an unsigned type whereby your subtraction wraps "below" zero. The result will be a very large number.

One way you can fix your equation is to cast the result to int:

abs(h - static_cast<int>(Board[h].size())   1)

But in this case, simply adjusting the equation to not wrap is better:

Board[h].size() - h -  1

CodePudding user response:

The first error I saw when compiling the code is:

temp.cpp:37:42: error: call to 'abs' is ambiguous

... i.e. the compiler has multiple versions of the the abs function that it could call, and it doesn't know which one it ought to use:

note: candidate function
inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) 
                                      ^
note: candidate function
inline _LIBCPP_INLINE_VISIBILITY long long abs(long long __x) 
                                           ^
note: candidate function
inline _LIBCPP_INLINE_VISIBILITY float abs(float __lcpp_x) 
                                       ^
note: candidate function
inline _LIBCPP_INLINE_VISIBILITY double abs(double __lcpp_x) 
                                        ^
note: candidate function
abs(long double __lcpp_x) 

... that's easy enough to fix, we'll just cast the argument to one of the types that abs explicitly handles:

abs(static_cast<long>(h - Board[h].size()   1))

The second error is this:

temp.cpp:37:82: error: invalid operands to binary expression ('unsigned long' and 'std::string' (aka 'basic_string<char>'))

... caused because you are trying to compare a string to an integer-type, which doesn't make sense. Presumably you wanted to convert the string to an integer first, so you could then compare that:

cin >> playerInput; 
const int playerInputInt = atoi(playerInput.c_str());

[...]

if(Board[h].size() * abs(static_cast<long>(h - Board[h].size()   1))   d == playerInputInt && Board[h][d] == " ") {...}
  •  Tags:  
  • c
  • Related