code bugs out? wherE?

the following code creates a pyramid with * and "spaces" then ads an 'o' object that descents depending on the result of a random generated number divided by 2,

the problem is that my object only correctly advances for the first two lines, then stops going left or right...

help?

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
#include <dos.h>
#include <stdio.h>

using namespace std;



int random(int a, int b)
{
	return a+rand()%(b-a);
}


void fo(int t,int n, int x, int inicio)
{
	int re, f, c;
	re=0;

	char fondo[9][16];
	
	/* fills the entire array with spaces */
	

	for(f=0;f<9;f++)
	{
		for(c=0;c<16;c++)
		{
			fondo[f][c]=' ';
		}
	}
	

	
	/* places the * for the pyramid */
	
	fondo[1][8]='*';
	fondo[8][15]='*';
	fondo[2][7]='*';
	fondo[2][9]='*';
	fondo[3][6]='*';
	fondo[3][8]='*';
	fondo[3][10]='*';
	fondo[4][5]='*';
	fondo[4][7]='*';
	fondo[4][9]='*';
	fondo[4][11]='*';
	fondo[5][4]='*';
	fondo[5][6]='*';
	fondo[5][8]='*';
	fondo[5][10]='*';
	fondo[5][12]='*';
	fondo[6][3]='*';
	fondo[6][5]='*';
	fondo[6][7]='*';
	fondo[6][9]='*';
	fondo[6][11]='*';
	fondo[6][13]='*';
	fondo[7][2]='*';
	fondo[7][4]='*';
	fondo[7][6]='*';
	fondo[7][8]='*';
	fondo[7][10]='*';
	fondo[7][12]='*';
	fondo[7][14]='*';
	fondo[8][1]='*';
	fondo[8][3]='*';
	fondo[8][5]='*';
	fondo[8][7]='*';
	fondo[8][9]='*';
	fondo[8][11]='*';
	fondo[8][13]='*';
	fondo[8][15]='*';
	
		/* creates the object that should move down the pyramid*/
	if (x>0)
	{
		if (n%2==0)
		{
			inicio = inicio-1;
			fondo[x][inicio]='o';
		}
		else 
		{
			inicio = inicio+1;
			fondo[x][inicio]='o';
		}
	}
	else
	{
		fondo[x][inicio]='o';
	}

	/*prints the pyramid according to selected height (t)*/
	
	

	for(f=0;f<t+1;f++)
	{
		for(c=0;c<16;c++)
		{
			cout<<fondo[f][c]<<"  ";
		}
		cout<<endl;	
	}
}



/* delivers N and T to function fo*/
void pinta(int n, int t, int x, int inicio)
{
	
	
	if (t>8)
	{
		cout<<"you are over the max height, try again.";
	}
	else
	{
		fo(t,n,x,inicio );
	}
	
	
}


void main ()
{
	int t, x, tiempo, n, a, inicio;

	x=0;
	a=0;
	inicio=8;
	cout<<"select a height between 0 and 7 ";
	cin>>t;
	
	
	while(x<t)
	{
		for(x=0; x<t+1; x++)
		{
			
				tiempo=time(NULL);
				Sleep(2);
				system("CLS");
				srand(tiempo);
				n=random(a, 100);
				pinta(n,t,x, inicio);
				
			
		}
	}
	


	getch();
	
}


code has been embeded and remakrs translated into english (yes from spanish)

exactly, if user selects 0, nothing should happen - the idea is that T will be height of the pyramid or the number of "levels" it has,

n%2 is indeed a module, but if result = 0 it means that N is a pair number, and should tell the object to move down a level and a whole column (+1), it its not 0, then its an impair number and it should do +1 level -1 column;

random should be created for every level to have different movements of our object should it not? or do i still move srand to beginning of main?

thanks
Last edited on
Please put the code in code tags: [code]Your code[/code]

call srand(time(NULL)) only once at the begining of main. This way rand() returns always the same value for one second

This while(x<t) doesn't make sense. if the user enters 0 nothing happens. Otherwise it's guaranteed just on cycle.

Hm, is it spanish? It's hard to read like that.

You are aware that this n%2 is not division but a modulo?
hmmm i cant post? edited original with said changes
Don't change things that works for you.

Well, now it's better to see.

The reason for your problem is the the variable 'inicio' doesn't change outside fo(). If you want to change it outside you need to pass it as a reference: int &inicio // Note the &
ok, ill try and change it later after my upcoming test and see if it works;

ill add the if that changes 'inicio' (means start) into main,

thanks
negative,

object still drops in a straight line
this codes cleared the object falling in a straight line but now it just follows one of the diagonals depending on the random.... it will never fall inside the triangle lol.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include <Windows.h>


using namespace std;



int random(int a, int b)
{
	return a+rand()%(b-a);
}


void fo(int t,int n, int x, int inicio)
{
	int f, c;
	

	char fondo[9][16];
	
	/* crea toda la piramide con espacios */
	

	for(f=0;f<9;f++)
	{
		for(c=0;c<16;c++)
		{
			fondo[f][c]=' ';
			fondo[x][inicio]='o';
		}
	}
	

	
	/* crea todos los * de la piramide */
	
	fondo[1][8]='*';
	fondo[8][15]='*';
	fondo[2][7]='*';
	fondo[2][9]='*';
	fondo[3][6]='*';
	fondo[3][8]='*';
	fondo[3][10]='*';
	fondo[4][5]='*';
	fondo[4][7]='*';
	fondo[4][9]='*';
	fondo[4][11]='*';
	fondo[5][4]='*';
	fondo[5][6]='*';
	fondo[5][8]='*';
	fondo[5][10]='*';
	fondo[5][12]='*';
	fondo[6][3]='*';
	fondo[6][5]='*';
	fondo[6][7]='*';
	fondo[6][9]='*';
	fondo[6][11]='*';
	fondo[6][13]='*';
	fondo[7][2]='*';
	fondo[7][4]='*';
	fondo[7][6]='*';
	fondo[7][8]='*';
	fondo[7][10]='*';
	fondo[7][12]='*';
	fondo[7][14]='*';
	fondo[8][1]='*';
	fondo[8][3]='*';
	fondo[8][5]='*';
	fondo[8][7]='*';
	fondo[8][9]='*';
	fondo[8][11]='*';
	fondo[8][13]='*';
	fondo[8][15]='*';
	
	


	/*Pone en pantalla la piramide con tamaƱo seleccionado */
	

	for(f=0;f<t+1;f++)
	{
		for(c=0;c<16;c++)
		{
			

			cout<<fondo[f][c]<<"  ";
		}
		cout<<endl;	
	}
}



/* entrega variables N y T a fo */
void pinta(int n, int t, int x, int inicio)
{
	
	
	
		fo(t,n,x,inicio );
	}
	
	



void main ()
{
	int t, x, y, tiempo, n, a, b, tamanio, r, d, inicio;

	x=0;
	
	cout<<"introduce un numero de filas entre 0 y 7 para definir la altura de tu piramide: ";
	cin>>t;

	if (t>8)
	{
		cout<<"superaste el limite, vuelve a intentarlo.";
	}
	else
	{
	
	while(x<t)
	{
		for(x=0; x<t+1; x++)
		{
			if (x>0)
	{
		if (n%2==0)
		{
		
			inicio=inicio+1;
		}
		else 
		{
		
			inicio=inicio-1;
			
		}
	}
	else
	{
		inicio=8;
	}

				tiempo=time(NULL);
				srand(tiempo);
				system("cls");
				n=random(1, 100);
				pinta(n,t,x,inicio);
				
			
		}
	}
	}


	getch();
	
}

Topic archived. No new replies allowed.