Issue in a simple program
Feb 4, 2014 at 3:22pm UTC
I try to get a random character to be in a map I defined, it doesn't let me;
problem in the 'random', it says "Expression must have constant value"
Any help?
How do I get 'o' to be in a random place at the map within the borders?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include "stdafx.h"
#include <iostream>
#include "windows.h"
using namespace std;
char map[10][20] = {
"###################" ,
"#X #" ,
"# #" ,
"# #" ,
"# #" ,
"# #" ,
"# #" ,
"# #" ,
"###################"
};
bool game_running = true ;
int x=1;
int y=1;
int randomY=rand() % 10;
int randomX=rand() % 20;
map[randomY][randomX] = 'o' ;
int main()
{
while (game_running == true ){
system("cls" );
for (int display=0; display<10; display++){
cout << map[display] << endl;
}
system("PAUSE>NUL" );
if (GetAsyncKeyState(VK_DOWN)){
int y2 = y+1;
if (map[y2][x]== ' ' )
{
map[y][x] = ' ' ;
y++;
map[y][x]= 'X' ;
}
}
if (GetAsyncKeyState(VK_UP)){
int y2 = y-1;
if (map[y2][x]== ' ' )
{
map[y][x] = ' ' ;
y--;
map[y][x]= 'X' ;
}
}
if (GetAsyncKeyState(VK_RIGHT)){
int x2 = x+1;
if (map[y][x2] == ' ' )
{
map[y][x] = ' ' ;
x++;
map[y][x]= 'X' ;
}
}
if (GetAsyncKeyState(VK_LEFT)){
int x2 = x-1;
if (map[y][x2] == ' ' )
{
map[y][x] = ' ' ;
x--;
map[y][x]= 'X' ;
}
}
}
return 0;
}
Feb 4, 2014 at 3:46pm UTC
put at least line 26 inside the body of main()
Feb 4, 2014 at 3:51pm UTC
You can't put a statement like line 26 in the global scope; when would it be executed? Try putting it at the start of main() instead.
Feb 4, 2014 at 8:48pm UTC
So I put it on the inside, but now it's always in the same spot.
any ideas?
Feb 6, 2014 at 6:59am UTC
put 24,25,26 right after line 31
Topic archived. No new replies allowed.