why doesnt it work?


#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
void wait ( int );

using namespace std;
int main ()
{
int n;
int m;
int j;
char pista1[n];
int o;
int q;
char tasta;



cout<<"introduce the length"<<endl;
cin>>n;
j=0;
pista1[0]='*';
pista1[n-1]='*';
pista1[n/2]='o';
pista1[m]='^';
while (j<n-1);
{
if (pista1[j]!='^')
{
if (pista1[j]!='*')
if (pista1[j]!='o')
pista1[j]=' ';
}
j=j+1;


}


cout<<(ios::right|ios::left)<<pista1[n]<<endl;

m=pista1[1];
srand ( time(NULL) );
o=rand();
m=o;

while (o>n-1)
{
srand ( time(NULL) );
o=rand();
}


system("PAUSE");
return 0;
}



it should write " * O * "
depending on the length
but it doesnt show anything t o me
why?
For starters:
1
2
char pista1[n]; // at this point, n is not initialized
pista1[m]='^'; // neither is m 


Both n and m should both be assigned a value before you use them. I'm not sure what you're trying to do here, but I have a feeling you're making it more complicated than it has to be.
Last edited on
first i don't think i know how to initialize them please tell me how to initialize them
secondly i will tell you what do i have to do cause you may help me even more i have to make a small game. it should look like that:
* o *
* o ^ *
and so on. here o is the player and ^ are obstacles and * the wall. the * * should change by themselves and ^ the same, they should appear aleatory and the game stops when the o hits a wall or ^. and the speed with which the lines appear should grow every 10 lines. and when the game is over it should write the points i earned. and i think i should write you the last version of the program (of course, it doesn't work but you may tell me whats wrong with it ) and dont forget that each pieceo f advice is extremely useful for me
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
void wait ( int );

using namespace std;
int main ()
{
int PozitieJucator;
int PrimaSteluta;
int n;
int m;
int j;
char pista1[n];
int o;
int q;
char tasta;
void wait ( int );
int TimpAsteptare;
int ModificarePista;
int aleator;
int punctaj;
int asteptare;


punctaj=0;
asteptare=120;
cout<<"introduceti latimea pistei"<<endl;
cin>>n;
j=0;
pista1[PrimaSteluta]='*';
pista1[n-1]='*';
pista1[PozitieJucator]='o';
cout<<(ios::right|ios::left)<<pista1[n]<<endl;
while (j<n-1);
{
if (pista1[j]!='^')
{
if (pista1[j]!='*')
if (pista1[j]!='o')
pista1[j]=' ';
}
j=j+1;


}




m=pista1[1];
srand ( time(NULL) );
o=rand();
m=o;

while (o)
{
srand ( time(NULL) );
o=rand();
if (0>PrimaSteluta)
if (o<n-1);
pista1[0]='^';

}
ModificarePista=0;
while (ModificarePista)
{
srand ( time(NULL) );
aleator=rand();
if (aleator % 2!=0)

{ if (ModificarePista<7)
{

ModificarePista=ModificarePista+1;
n=n+1;
PrimaSteluta=PrimaSteluta+1;
PozitieJucator=PozitieJucator+1;
}

if (ModificarePista>=0)
{
ModificarePista=ModificarePista-1;
n=n-1;
PrimaSteluta=PrimaSteluta-1;
PozitieJucator=PozitieJucator+1;
}

}


do
{
if( kbhit() )
{
tasta = getch();
if (tasta='a')
{
PozitieJucator=PozitieJucator-1;


if (tasta='d')
PozitieJucator=PozitieJucator+1;
}
}

}


while (pista1[PozitieJucator]);

{

if (pista1[PozitieJucator]='*') cout<<"Jocul s-a terminat si ati obtinut"<<punctaj<<"puncte"<<endl;
if (pista1[PozitieJucator]='^') cout<<"Jocul s-a terminat si ati obtinut"<<punctaj<<"puncte"<<endl;
if (pista1[PozitieJucator]=' ')

if (n%10!=0)

wait(asteptare);
punctaj=punctaj+1;
cout<<(ios::right|ios::left)<<pista1[n]<<endl;

if (n%10==0)

asteptare=asteptare-asteptare/12;
wait(asteptare);
punctaj=punctaj+1;
cout<<(ios::right|ios::left)<<pista1[n]<<endl;



}


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

void wait ( int miliseconds )
{
clock_t endwait;
endwait = clock () + miliseconds * CLOCKS_PER_SEC / 1000;
while (clock() < endwait) {}
}

I mean that you're creating an array[n] without first giving n a value.

1
2
int n;
char pista1[n]; // what's n? 


Try this:
1
2
3
4
5
6
7
8
9
cout<<"introduce the length"<<endl;
cin >> n; // now we know what n is

char* pista = new char[n];
char pista1[n+1]; // safe to create the array

// *do stuff*

delete[] pista; // don't forget 



This looks kinda iffy too:
1
2
3
4
5
6
7
8
do {
// your code
}

while (pista1[PozitieJucator]); // ends the do while loop

{ // does another loop start here? what are its conditions?
// more code 


You're also trying to assign a value to pista1[m] without knowing what m is. It's kinda hard to understand your code since it has no comments, the variable names are either single letters (a, b, c) or words in a language that I don't understand.

I suggest you read another basic tutorial or two on C++, as you don't seem to have fully grasped some parts such as loops.





EDIT: Changed the code a bit.
Last edited on
Topic archived. No new replies allowed.