Thread: what am i doing wrong

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    what am i doing wrong

    could some one look at the program and tell me what i am doing wrong all i want to do is to make a character x and move i around


    #include <iostream.h>
    #include <stdlib.h>

    char map[8][8];
    char possition[x][y];
    int x=1;
    int y=1;
    char command;
    main()
    {
    cout<<map[1][1]<<map[1][2]<<map[1][3]<<map[1][4]<<map[1][5]<<map[1][6]<<map[1][7]<<map[1][8];
    cout<<map[2][1]<<map[2][2]<<map[2][3]<<map[2][4]<<map[2][5]<<map[2][6]<<map[2][7]<<map[2][8];
    cout<<map[3][1]<<map[3][2]<<map[3][3]<<map[3][4]<<map[3][5]<<map[3][6]<<map[3][7]<<map[3][8];
    cout<<map[4][1]<<map[4][2]<<map[4][3]<<map[4][4]<<map[4][5]<<map[4][6]<<map[4][7]<<map[4][8];
    cout<<map[5][1]<<map[5][2]<<map[5][3]<<map[5][4]<<map[5][5]<<map[5][6]<<map[5][7]<<map[5][8];
    cout<<map[6][1]<<map[6][2]<<map[6][3]<<map[6][4]<<map[6][5]<<map[6][6]<<map[6][7]<<map[6][8];
    cout<<map[7][1]<<map[7][2]<<map[7][3]<<map[7][4]<<map[7][5]<<map[7][6]<<map[7][7]<<map[7][8];
    cout<<map[8][1]<<map[8][2]<<map[8][3]<<map[8][4]<<map[8][5]<<map[8][6]<<map[8][7]<<map[8][8];

    cout<<"enter a direction\n.";
    cin>>command;

    while (command!='exit')
    {
    switch (command)
    {
    case 'e':
    (map[x][y] = map[x][y++]);
    break;
    case 'd':
    (map[x][y] = map[x][y--]);
    break;
    case 'f':
    (map[x][y] = map[x++][y]);
    break;
    case 's':
    (map[x][y] = map[x--][y], cout<<map[x][y]<<"X");
    break;
    default:
    cout<<"bad input"
    break;
    }
    }
    else
    system("pause");
    return 0;
    }

  2. #2
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    you can start by actually telling the compiler what the arrays are:

    char map[8][8];
    cout<<map[1][1]; //what the heck?

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    hes right, first you need to initilize the map array, use some imbedded for's

    for(int x = 0; x < 8; x++) {
    for(int y = 0; y < 8; y++) {
    map[x][y] = 0;
    }
    }

    Now you need to put the cout statement that is displaying the grid inside the loop, where it is now it will only be displayed one time.

    your code to move the focus is wrong,

    case 'e':
    map[x][y] = 0;
    y++;
    map[x][y] = 1;
    break;

    now since the map array is of type int you will only be able to display numbers,

    I would replace that part of code with a function and a set of imbedded for's

    I didnt test this, but it should work.

    void DrawMapLocation(int); //function decleration

    for(int y = 0; y < 8; y++) {
    cout << endl;
    for(int x = 0; x < 8; x++) {
    DrawMapLocation(map[x][y]);
    }
    }

    void DrawMapLocation(int i) {
    if(i != 0) cout << 'X';
    else cout << " ";
    }


    There will also be one more problem, your grid will be redisplayed below the last one, you will need to add in a series of newline's or look up a function to clear the console window.

    Hope i helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM