Thread: i need some help ><

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    i need some help ><

    hi everybody i'm new to this msg board
    currently i'm working on a task which involves mazing
    i have learnt C++ for a month and must say i am not very good at it
    well while i was coding, my friend took a look at my codes and told me i have a major mistake and ever since then i've been staring at the script and cant figure it out
    can someone help me out here? by the way this is not complete
    Code:
    #include <iostream.h>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int seekRoad(char [4][6], int, int);
    
    int main() 
    {
             
        char maze[][6] = {{'S', 'R', 'X', 'X', 'X', 'X'},
                           {'X', 'R', 'X', 'X', 'R', 'E'},
                           {'X', 'R', 'R', 'X', 'R', 'X'},
                           {'X', 'X', 'R', 'R', 'R', 'X'}};
    
    
        cout << "Start X = 0\n";
        cout << "Start Y = 0\n\n";
    
        int result = seekRoad(maze, 0, 0);
        if (result == -1) {
          cout << "No exit!\n";
        if (maze [x][y+1] =="R" );  
      else if (maze [x][y-1] == "R");
      else if (maze [x+1][y] == "R");
      else if (maze [x-1][y] == "R");
        }
    
        return 0;
    }
    Last edited by kid1412; 10-26-2006 at 10:05 AM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    else if (maze [x][y-1] == "R");
      else if (maze [x+1][y] == "R");
      else if (maze [x-1][y] == "R");
    One thing, maze is an array of chars, so you should be comparing maze[x][y] with character 'R'.

    Another thing is, you'll go out of bounds here. For example x-1 could easily be -1.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    int seekRoad(char [4][6], int, int);
    You can't make an array parameter. How would you pass data to it?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by maxorator
    Code:
    int seekRoad(char [4][6], int, int);
    You can't make an array parameter. How would you pass data to it?
    Considering that is the prototype, it's perfectly fine. I believe it's also valid to leave arguments unnamed if they aren't referenced inside the function, e.g.:
    Code:
    void foo(int)
    {
      std::cout<<"Foo";
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by maxorator
    Code:
    int seekRoad(char [4][6], int, int);
    You can't make an array parameter. How would you pass data to it?
    i'm sorry but what do you mean by that
    so what am i supposed to do to fix it?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    if (maze [x][y+1] =="R" );
    else if (maze [x][y-1] == "R");
    else if (maze [x+1][y] == "R");
    else if (maze [x-1][y] == "R");

    You need to put these lines (with some of the changes already mentioned) in a new function called seekRoad.

    Maybe
    Code:
    int seekRoad(char maze[4][6], int x, int y) {
      // more here
        if (maze [x][y+1] =="R" );  
      else if (maze [x][y-1] == "R");
      else if (maze [x+1][y] == "R");
      else if (maze [x-1][y] == "R");
      // more here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    ok i worked and played a bit with it and after reading the comments i did a lil alteration on my codings
    but now i have another problem
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int y = 1;
    int x = 2;
    
    int seekRoad(char [][6], int x, int y);
    
    int main() 
    {
    
             
        char maze[][6] = {{'S', 'R', 'X', 'X', 'X', 'X'},
                           {'X', 'R', 'X', 'X', 'R', 'E'},
                           {'X', 'R', 'R', 'X', 'R', 'X'},
                           {'X', 'X', 'R', 'R', 'R', 'X'}};
    
    
        cout << "Start X = 0\n";
        cout << "Start Y = 0\n\n";
    void runMaze(int, int);
    
        int result = seekRoad(maze, 0, 0);
        if (result == -1) {
          cout << "No exit!\n";
    
        }
        {if( (x<=4 && y<=6 )) {
    {if( maze[x][y] == 'E' ) return 0;
        {if (maze [x][y+1] =="R" )return 0;  
      else if (maze [x][y-1] == "R")return 0;
      else if (maze [x+1][y] == "R")return 0;
      else if (maze [x-1][y] == "R")return 0;
        return 0;}}}
    }}
    i keep getting a ISO C++ forbids comparision between pointer and interger

    what does that mean and how do i get rid of it? D:

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if (maze [x][y+1] =="R" )return 0;  
      else if (maze [x][y-1] == "R")return 0;
      else if (maze [x+1][y] == "R")return 0;
      else if (maze [x-1][y] == "R")return 0;
    "R" is a pointer to an array of char.
    all the "R" should be 'R'
    Kurt

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    [Linker error] undefined reference to `seekRoad(char (*) [6], int, int)'

    what does this mean?

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd say it means that there is no function implemented that is called seekRoad, returns an int and takes an array of array of characters of length 6 and two ints as parameters.
    BTW:
    declaring
    Code:
    void runMaze(int, int);
    inside of main() looks quite useless to me.

    Kurt

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    ok so in order to get ride of that problem
    i have to
    1) find a function that fits that description for it?
    or?
    is there another way i get get around it?

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What do you mean that you have to find one ?
    You declared it, you called it so you have to implement it as well.
    Kurt

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    ok i got into gear
    and got some stuff down
    and this is what i have
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int seekRoad(char [][6], int, int);
    
    int main()
    {
        char maze[4][6]={{'S', 'R', 'X', 'X', 'X', 'X'},
                         {'X', 'R', 'X', 'X', 'R', 'E'},
                         {'X', 'R', 'R', 'X', 'R', 'X'},
                         {'X', 'X', 'R', 'R', 'R', 'X'}};
                         
        cout << "Start X = 0\n";
        cout << "Start Y = 0\n\n";
        cout << maze[0][0] << endl;
        
        int result = seekRoad(maze, 0, 0);
        if (result == -1){
           cout << "No exit!\n";}
        else if (result == 1)
                cout << "Good\n";
        else
           cout << "Error\n";
        system("PAUSE"); 
        
        return 0;
    }
    int seekRoad(char maze[][6], int x, int y)
    {  {if( (x<=4 && y<=6 ));}
        if (maze[x][y]=='E')
           return -1;
        else if (maze[x][y+1]=='R'){
             maze[x][y]='o';
             y++;
             cout << "current X = " << x << endl;
             cout << "current Y = " << y <<endl;
             cout << maze[x][y]<< endl;
             cout << maze[x][y-1] <<endl;
             return 1;}
        else
           return 2;
    }
    another problem occured
    when i script above, it works (only one cell though but none the less its still better than the prototype) but when i include
    Code:
     else if (maze [x][y-1] == 'R');
      else if (maze [x+1][y] == 'R');
      else if (maze [x-1][y] == 'R')
    to the end of
    Code:
     else if (maze[x][y+1]=='R'){
    and run the file, it produces an error and i dont understand why
    can anyone help me?

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    When you use some index - you should be sure - that it inside the bounds.

    When you retreive the value on index x-1 - make sure it is >=0 and < maxwidth

    If you found an empty space to the left - shouldn't you move there?

    Seeing your code you do nothing in the other 3 cases except the implemented one...

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    ok so i finished the work
    but then even if the maze doesnt work i still get the comment "good"
    what went wrong ><
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int seekRoad(char [][6], int, int);
    
    int main()
    {
        char maze[4][6]={{'S', 'R', 'X', 'X', 'X', 'X'},
                         {'X', 'R', 'X', 'X', 'R', 'E'},
                         {'X', 'R', 'R', 'X', 'R', 'X'},
                         {'X', 'X', 'R', 'R', 'R', 'X'}};
                         
        cout << "Start X = 0\n";
        cout << "Start Y = 0\n\n";
        cout << maze[0][0] << endl;
        
        int result = seekRoad(maze, 0, 0);
        if (result == -1){
           cout << "No exit!\n";}
        else if (result == 1)
                cout << "Good\n";
        else
           cout << "Error\n";
        system("PAUSE"); 
        
        return 0;
       
    }
    int seekRoad(char maze[][6], int x, int y)
    {
        if (maze[x][y]=='E')
           return -1;
      else if (maze[x][y+1]=='R'){
             maze[x][y]='o';
             y++;
             cout << "current X = " << x << endl;
             cout << "current Y = " << y << "\n\n";
             seekRoad(maze, x, y);
             return 1;}
     else if (maze[x+1][y]=='R'){
             maze[x][y]='o';
             x++;
             cout << "current X = " << x << endl;
             cout << "current Y = " << y << "\n\n";
             seekRoad(maze, x, y);
             return 1;}
                 else if (maze[x][y-1]=='R'){
             maze[x][y]='o';
             y--;
             cout << "current X = " << x << endl;
             cout << "current Y = " << y << "\n\n";
             seekRoad(maze, x, y);
             return 1;}
        else if (maze[x-1][y]=='R'){
             maze[x][y]='o';
             x--;
             cout << "current X = " << x << endl;
             cout << "current Y = " << y << "\n\n";
             seekRoad(maze, x, y);
             return 1;}
        else if (maze[x][y+1]=='E'){
             maze[x][y]='o';
             y++;
             cout << "Exit X = " << x << endl;
             cout << "Exit Y = " << y << "\n\n";
             return 1;}
        else if (maze[x+1][y]=='E'){
             maze[x][y]='o';
             x++;
             cout << "Exit X = " << x << endl;
             cout << "Exit Y = " << y << "\n\n";
             return 1;}
        else if (maze[x][y-1]=='E'){
             maze[x][y]='o';
             y--;
             cout << "Exit X = " << x << endl;
             cout << "Exit Y = " << y << "\n\n";
             return 1;}
        else if (maze[x-1][y]=='E'){
             maze[x][y]='o';
             x--;
             cout << "Exit X = " << x << endl;
             cout << "Exit Y = " << y << "\n\n";
             return 1;}
    
        else
           return 2;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. primitive question ><
    By MicroFiend in forum Game Programming
    Replies: 5
    Last Post: 10-14-2005, 10:14 AM
  2. STEP turbo C 3.0 >< Borland 5.02
    By Huh..... in forum C Programming
    Replies: 0
    Last Post: 03-12-2002, 07:54 AM
  3. searching ><
    By hyaline in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 05:29 AM