what is wrong with this array

i want to make a game. i tried to write something but it doesn't work please tell me what is wrong

// this program is a game but i t doesnt work
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void wait ( int );

using namespace std;
int main ()
{
int Player;
int FirstStar;
int ArrayLength;

int j;

int obstacle;
int q;
char key;
void wait ( int );
int time;
int ArrayChange;
int random;
int Paints;





cout<<"introduceti latimea pistei"<<endl;
cin>>length; // this should read the length of the array which is introduced fro mthe keyboard
char array[length]; // this should be the array with the lenthg i introduced

FirstStar=0;
array[FirstStar]='*'; // this should make the first char on the first array to be *
array[length-1]='*'; // this should make the last char to be a *
player= (length-1)/2;
array[player]='o' // at the middle is o which is the char i should move through the game


j=0
while (j<length-1); //through this while i want to check each char of the array and if it is different from 'o' or '*' or '^' it should be ' '
{
if (j!=FirstStar)
{
if (j!='0')
if (j!='*')
array[j]= ' ';

}
j=j+1;


}


cout<<(ios::right|ios::left)<<array<<endl; //this should write the array i the middle of the screen as i described until now

// now i want to make the following lines to have each an aleatory obstacle
srand ( time(NULL) );
obstacle=rand(); // then it should find a random number


while (o)
{

if (o>FirstStar) //then i want to ensure that
if (o<length-1); //the obstacle is between the first and the last star
pista1[o]='^';

}
ArrayCange=0;
while (ArrayChange) // then i want to make each line to change it's position with one space to the right or tot the left
{
srand ( time(NULL) );
random=rand(); // then it should find a random number
if (aleator % 2!=0) //if the random nr is divided by 2 then the next array sould be one space to the left

{ if (ArrayChange<7) // then i want to ensure that the arrays dont go out of the screen
{

ArrayChange=ArrayChange+1; //then it calculates how much was the array movineg
length=length+1; // then the length changes
FirstStar=FirstStar+1; // then the first star goes one ' ' to the right and instead of it should remain ' '

}



if (ModificarePista>=0) // i want to ensure that the array doesnt become smaller
{
ArrayChange=ArrayChange-1; //the same as up
length=length-1; //the same as up
FirstStar=FIrstStar-1; //the same as up

}

}


do
{
if( kbhit() ) // this should allow me to introduce the keys through which i may be able to control the 'o'
{
key = getch();
if (key='a') // if i press a the 'o'
{
player=player-1; // then o goes to the left

}
if (tasta='d')
{
player=player+1; // then 'o' goes to the right
}
else player=player // else it doesnt change the position
}

}
time = 120 ; // this should make the program wait
points =0; // at the beginning you have 0 points
while (array[player]); // through this while i want to make the program recognise

{

if (player=random) cout<<"GAME OVER! YOU OBTAINED"<<points<<"POINTS"<<endl; // through this if the o thouches a '^'
if (player=FirstStar) cout<<"GAME OVER! YOU OBTAINED "<<points<<"POINTS"<<endl; // or the first "*"
if (array[player]=length-1) cout<<"GAME OVER! YOU OBTAINED "<<points<<"POINTS"<<endl; // or the last one it should stop and write the points i earned

if (length%10!=0) // through this i want to make the wait time to reamin unchanged

wait(time);
points=points+1;
cout<<(ios::right|ios::left)<<array<<endl;

if (LungimePista%10==0) // i want to make the wait time be smaller each 10 arrays

time=time-time/12;
wait(time);
points=points+1;
cout<<(ios::right|ios::left)<<array<<endl;



}


system("PAUSE");
return 0;
}
}

void wait ( int miliseconds ) // this is a function which should make the rows to appear at the speed i want
{
clock_t endwait;
endwait = clock () + miliseconds * CLOCKS_PER_SEC / 1000;
while (clock() < endwait) {}
}










- Use the #format when uploading code

- You have to declare every variable before using them (length, array,player etc.)

- The size of an array needs to be constant, either a simple number int array[5] or a constant variable:
1
2
const int size=5;
int array[size]

If you want to declare the size of an array during runtime, you will have to use dynamic memory: http://www.cplusplus.com/doc/tutorial/dynamic.html

Ps. This are the first mistakes I came across, there may be more. Also, I haven't really looked at how you're program works. So there may be easier solotions as using dynamic memory. A lot of things can be done using a for-loop or someting like that.
well i didn't know how to write # format. please tell me how to write it
secondly i declared all declared all the variables i introduced. i didn't receive any error cause of it. My program works but it doesn't what it should do.

lastly i don't think that it a dynamic memory is useful in my case. i don't want me to modify the size of array during run time. i want it to change by itself.
my array should look like that:
*--------------o-----------------*
-*-------------o------------------*
here - are spaces ' ' and i wrote them cause else ' ' dissapear when i post it
i want it to change by self and i want to do it by introducing a ' ' after each row. if there is any other way to do it please tell me.
moreover it is something else that i don't know. in this game it should appear obstacle during running the program. the last two lines should look like this:
the line before the last should contain o which is the player and ^ which is an obstacle. i may control the o where to go on the next row and the next row should show just obstacle and after i move o to the right or left it the last line should contain o there where i moved it and it should appear a new line which does contain the same, just obstacle. that's what i don't know how to make the program replace the last line and appear a new one as i described.
PS: i have to finish it soon so each advice you give me may beextremely useful


Last edited on
cin>>length;

Length is not declared yet.
Regarding the # format:
when creating/editing a post, on the left you should see somethihg like this:
Format:
# "
B I U S
TT ≣
X2 X2

By clicking on the # button you should see the [code][/code] tags.
If you type between them, your code will look like this:
1
2
3
//some code
using code::tags;
this->is("formatted source code");
If you want to see how your message looks like before posting it, you can use the 'preview'-button under the text-box. May come in handy if you want to see where you can use the other formats for.
Last edited on
Topic archived. No new replies allowed.