Help - Search - Members - Calendar
Full Version: C++ Source Code Help
CyberiaPC.com Community > Technical Zone > Programming (Java SE/EE, VB, C#)
CheeseKebab
hi i'm having alot of problems, i have attempeted this assignment and it is so that a child(*) can move around a 9x9 Grid ,,,can some one debug this code for me please ...i don't know how to debug it...and why it won't work please help us

CODE
#include <stdlib.h>
#include <iostream>
using namespace std;

//Structures

struct pos
{
   int X;
    int Y;
};

struct directions
{
    bool Left;
    bool Up;
    bool Right;
    bool Down;
};


//Functions

pos MoveChild(pos Postion, int Counter[]);
int PrintGrid(pos Postion);
directions PossibleDirections(pos Position);

//Code

int main ()
{
    pos Position = { 4, 4 };          //Postion of Child
   int Moves = 0;          //The Number of moves user specifies
    int MovesCounter[4] = { 0 };      //This Counts how many moves the program has made


   while (!((Moves > 0) && (Moves <= 100)))      //Asks for input between 1 and 100  
   {
 cout << "\nPlease enter the number of times the Child is to move around: ";
 cin >> Moves;
 if (Moves > 100)
     cout << "\nYou have exceeded the limit of 100. Please try again!\n";
 if (cin.fail())            //clears the input stream if invalid input was entered
 
 {
     cin.clear();        //resets failbit bit
     char tmpr;
     while (cin.get(tmpr))      //Goes through all the input until it reaches an enter
     {
   if (tmpr == '\n')
       break;        //quits the loop
     }
 }
    }

    
    PrintGrid(Position);        //Prints intial grid
    for (int i = 0; i < Moves; i++)      //loops 'Moves' times
   {
 for (double long j=1; j<99999999;j++)
 {
     int x = 0;
       }
 system ("cls");            //Slows down the PC

 Position = MoveChild(Position, MovesCounter);    //moves the baby
    }

 cout << "Moves the Child has made:\n";      //Prints out the results of movement
 
 cout << "Left: " <<MovesCounter[0] << endl;
 cout << "Up: " <<MovesCounter[1] << endl;
 cout << "Right: " <<MovesCounter[2] << endl;
 cout << "Down: " <<MovesCounter[3] <<endl;

 return 0;
}

pos moveBaby(pos Position, int Counter[])
{
    unsigned int Direction = 0;          //Storage for te random number
    bool CanGo = false            //flag that is used to puit the loop below
   directions CanGoWhere = PossibleDirections(Position); //variable that holds which direction the Child can go
    
    while (CanGo == false)
    {
 Direction = rand();

 if ((Direction >= 0) && (Direction < 8192) && (canGoWhere.Left == true))
 {    //Left
     Position.X -= 1;
     Counter[0] += 1;    //increment counter
     CanGo = true;  //flag that causes loop termination
 }
 else if ((Direction >= 8192) $$ (Direction < 16384) && (CanGoWhere.Up == true))
 {   //up
     Position.Y -= 1;
     Counter[1] += 1;    //increment counter
     CanGo = true;  //flag that causes loop termination
 }
 else if ((Direction >= 16384) && (Direction < 24576) && (CanGoWhere.Right == true))
 {   //Right
     Position.X += 1;
     Counter[2] += 1;    //Increment counter
     CanGo = true;  //flag that causes loop termination
 }
 else if ((Direction >= 24576) && (Direction < 32768) && (CanGoWhere.Down == true))
 {    //Down
     Position.Y += 1;
     Counter[3] += 1;    //increment counter
     CanGo = true;  //flag causes loop termination
 }
    }

    PrintGrid(Position);
    return Position;
}

int PrintGrid(pos Position)

{ system ("cls");    //Clear the screen

    for (int i = 0; i < 9; i++)
    {
 cout << "-------------------\n";       //prints the horizontal border
 for (int j = 0; j < 9; j++)
 {
     cout << "|";           //prints the vertical border

     if ((j == Position.X) && (i == Position.Y))    //checks if Child is on the current position
   
   cout << "*";          //prints the Childs Position
     else
   cout << " ";          //Prints the empty space
 }
 cout << "|\n";            //Prints the last vertical border
    }
    cout << "-------------------\n\n";        //prints the remaining horizontal border since the loop only does the beginning one of the column
    return 0;
}

directions PossibleDirections(pos Position)
{
    directions TempDirections;

    TempDirections.Left = true;          //sets up all variable to true meaning the Child can move in any direction
    TempDirections.Up = true;
    TempDirections.Right = true;
    TempDirections.Down = true;

    if (Position.X == 0)        //This is where the program checks to see where the Child can Go and sets the values as false if it cant go in that direction
 TempDirections.Left = false;
    else if (Position.X == 8)
 TempDirections.Right = false;
    if (Position.Y == 0)
 TempDirections.Up = false;
    else if (Position.Y == 8)
 TempDirections.Down = false;
    return TempDirections;
}


These are the errors

CODE
ERRORS

--------------------Configuration: CIT Project - Win32 Debug--------------------
Compiling...
Walk Through.cpp
U:\CIT Project\Walk Through.cpp(83) : error C2146: syntax error : missing ';' before identifier 'directions'
U:\CIT Project\Walk Through.cpp(83) : error C2146: syntax error : missing ';' before identifier 'CanGoWhere'
U:\CIT Project\Walk Through.cpp(83) : error C2275: 'directions' : illegal use of this type as an expression
       U:\CIT Project\Walk Through.cpp(14) : see declaration of 'directions'
U:\CIT Project\Walk Through.cpp(83) : error C2065: 'CanGoWhere' : undeclared identifier
U:\CIT Project\Walk Through.cpp(83) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct directions' (or there is no acceptable conversion)
U:\CIT Project\Walk Through.cpp(89) : error C2065: 'canGoWhere' : undeclared identifier
U:\CIT Project\Walk Through.cpp(89) : error C2228: left of '.Left' must have class/struct/union type
U:\CIT Project\Walk Through.cpp(95) : error C2146: syntax error : missing ')' before identifier '$$'
U:\CIT Project\Walk Through.cpp(95) : error C2065: '$$' : undeclared identifier
U:\CIT Project\Walk Through.cpp(95) : error C2228: left of '.Up' must have class/struct/union type
U:\CIT Project\Walk Through.cpp(95) : error C2059: syntax error : ')'
U:\CIT Project\Walk Through.cpp(96) : error C2143: syntax error : missing ';' before '{'
U:\CIT Project\Walk Through.cpp(101) : error C2181: illegal else without matching if
U:\CIT Project\Walk Through.cpp(101) : error C2228: left of '.Right' must have class/struct/union type
U:\CIT Project\Walk Through.cpp(107) : error C2228: left of '.Down' must have class/struct/union type
Error executing cl.exe.

Walk Through.obj - 15 error(s), 0 warning(s)


could someone debug this source code for me...please....
usr.c
you're missing two semicolons before those lines. I'm not sure why you're passing pos to your struct. You can use "." to set its data values.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.