Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:33:23+00:00 2026-06-14T20:33:23+00:00

I am getting these two errors while compiling but i dont understand what i

  • 0

I am getting these two errors while compiling but i dont understand what i didnt do right.

main.cpp|107|error: ‘sqrt’ was not declared in this scope|

main.cpp|107|error: ‘pow’ was not declared in this scope|

#include <iostream>
#include <Windows.h>
using namespace std;

struct Player
{
    int x, y;
    Player()
    {
        x = -1;
        y = -1;
    }
};

struct Ghost
{
    int x, y, direction;
    Ghost()
    {
        x = -1;
        y = -1;
        direction = 1;
    }
};

const char SYMBOL_EMPTY = ' ';
const char SYMBOL_PLAYER = '@';
const char SYMBOL_GHOST = 'G';
const char SYMBOL_WALL = '#';
const int MapDx = 10;
const int MapDy = 20;
const int GameSpeed = 100;
const int LEFT = 1;
const int RIGHT = 2;
const int UP = 3;
const int DOWN = 4;
int direction = RIGHT;

char map[10][20] =
{
    "###################",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "#                 #",
    "###################"
};

bool isValidPos(int x, int y)
{
    return (x >= 0 && x < MapDx && y >= 0 && y < MapDy);
}

bool movePlayer(Player &player, int x, int y)
{
    if (!isValidPos(x, y))
    {
        return false;
    }

    char ch = map[x][y];

    if(ch != SYMBOL_EMPTY)
    {
        return false;
    }

    if (isValidPos(player.x, player.y))
    {
        map[player.x][player.y] = SYMBOL_EMPTY;
    }
    player.x = x;
    player.y = y;
    map[player.x][player.y] = SYMBOL_PLAYER;
    return true;
}

bool moveGhost(Ghost &ghost, int x, int y)
{
    if (!isValidPos(x, y))
    {
        return false;
    }

    char ch = map[x][y];

    if (ch != SYMBOL_EMPTY)
    {
        return false;
    }

    if (isValidPos(ghost.x, ghost.y))
    {
        map[ghost.x][ghost.y] = SYMBOL_EMPTY;
    }
    ghost.x = x;
    ghost.y = y;
    map[ghost.x][ghost.y] = SYMBOL_GHOST;
    return true;
}

void GhostAI(Ghost &ghost, Player &player)
{
    double a = sqrt((pow((double) (ghost.x - 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //UP
    double b = sqrt((pow((double) (ghost.x + 1) - player.x, 2)) + pow((double) ghost.y - player.y, 2)); //DOWN
    double c = sqrt((pow((double) (ghost.y - 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //RIGHT
    double d = sqrt((pow((double) (ghost.y + 1) - player.x, 2)) + pow((double) ghost.x - player.y, 2)); //LEFT
    if(a < b && a <= c && a <= d && ghost.direction != DOWN) ghost.direction = UP;
    else if(b <= c && b <= d && ghost.direction != UP) ghost.direction = DOWN;
    else if(c < d && ghost.direction != LEFT) ghost.direction = RIGHT;
    else if(ghost.direction != RIGHT) ghost.direction = LEFT;
}

void showMap()
{
    for (int x = 0; x < MapDx; x++)
    {
        cout << map[x] << endl;
    }
}

void showPlayer(Player &player)
{
    cout << "\nPlayerX: " << player.x << endl;
    cout << "PlayerY: " << player.y << endl;
}

void gameLoop()
{
    Player player;
    Ghost ghosts[3];
    movePlayer(player, 1, 2);
    moveGhost(ghosts[0], 5, 2);
    moveGhost(ghosts[1], 5, 5);
    moveGhost(ghosts[2], 5, 8);
    while (true)
    {
        system("cls");
        showMap();
        showPlayer(player);
        if (GetAsyncKeyState(VK_UP))
        {
            direction = UP;
        }
        else if (GetAsyncKeyState(VK_DOWN))
        {
            direction = DOWN;
        }
        else if (GetAsyncKeyState(VK_LEFT))
        {
            direction = LEFT;
        }
        else if (GetAsyncKeyState(VK_RIGHT))
        {
            direction = RIGHT;
        }
        switch (direction)
        {
        case UP:
            movePlayer(player, player.x-1, player.y);
            break;
        case DOWN:
            movePlayer(player, player.x+1, player.y);
            break;
        case LEFT:
            movePlayer(player, player.x, player.y-1);
            break;
        case RIGHT:
            movePlayer(player, player.x, player.y+1);
            break;
        }
        for (int ghost = 0; ghost < 3; ghost++)
        {
            GhostAI(ghosts[ghost], player);
            switch (ghosts[ghost].direction)
            {
            case UP:
                moveGhost(ghosts[ghost], ghosts[ghost].x-1, ghosts[ghost].y);
                break;
            case DOWN:
                moveGhost(ghosts[ghost], ghosts[ghost].x+1, ghosts[ghost].y);
                break;
            case LEFT:
                moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y-1);
                break;
            case RIGHT:
                moveGhost(ghosts[ghost], ghosts[ghost].x, ghosts[ghost].y+1);
                break;
            }
        }
        Sleep(GameSpeed);
    }
}


int main()
{
    gameLoop();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:33:24+00:00Added an answer on June 14, 2026 at 8:33 pm

    You need to

    #include <cmath>
    

    It’s the header that declares those functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting these 2 errors in my code In function 'int main()': error:
I'm getting two compile errors: /home/sater/projects/MotionManager/videoProcess.cpp:11:1: error: a function-definition is not allowed here before
I've been getting these messages in apache error.log for quite a while: [client 217.197.152.228]
Using an opencart plugin I am getting these errors in my apache error log
Please dont mark this as duplicate I am getting this error while debugging my
Getting message expected identifier in red for below these two statements } else if
Getting these errors in some java code http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html Exception in thread Thread-0 java.lang.RuntimeException: Cannot
Hello I am getting these LNK errors when I try to compile my MySQL
I've been getting these errors intermittently from sending email from my django app. I
I am getting this error when i visit my page: Caught AttributeError while rendering:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.