I think I went in the wrong direction

Pages: 12
I needed to create a grid so I did


#include <iostream>

using namespace std;

int main()
{
int length;
int hieght;
int row;
int collom;
double per;
double w;
double u;
double p;

row = 0;
collom = 0;

cout << "How long do you want your grid? ";
cin >> length;

cout << "How tall do you want your grid? ";
cin >> hieght;

cout << endl;

cout << "What percentage of nodes do you want? ";
cin >> per;
cout << endl;

w = length*hieght;

cout << "Out of " << w << " grid markers, you have ";
u = (per/100);
p = w * u;



int rounded = static_cast<int>(p+.5);


cout << rounded << " nodes." << endl;

do
{
do
{
if (length < 40)
{
cout << ". ";
}
else
{
cout <<".";
}
row++;
}
while (row < length);

cout << endl;
collom++;
row = 0;
}
while (collom < hieght);

cout << endl;

return 0;
}



but now I need to assign a (x,y) title to the grid.
I think I messed it up really bad, but I don't know another way to do a grid if I did.
Well I just read this quickly, but why dont you assign the height and length to an array and then print the array?

Sorry if this is wrong/not what you are looking for

Arcadiu
this may sound stupid but ???ARRAY???
Oh No I'm stuck again

#include <iostream>

using namespace std;

int main()
{
int length;
int hieght;
int row;
int collom;
double per;
double w;
double u;
double p;


row = 0;
collom = 0;

cout << "How long do you want your grid? ";
cin >> length;

cout << "How tall do you want your grid? ";
cin >> hieght;

cout << endl;

cout << "What percentage of nodes do you want? ";
cin >> per;
cout << endl;

w = length*hieght;

cout << "Out of " << w << " grid markers, you have ";
u = (per/100);
p = w * u;

int rounded = static_cast<int>(p+.5);

cout << rounded << " nodes." << endl;

hieght = hieght - 1;
length = length - 1;

char grid [hieght][length];

grid [0][0]=*;

cout << endl;

return 0;
}



error messages

Error 1 error C2065: 'hieght' : undeclared identifier u:\visual studio 2008\projects\grid\grid\grid.cpp 4 grid

Error 2 error C2065: 'length' : undeclared identifier u:\visual studio 2008\projects\grid\grid\grid.cpp 4 grid

Error 3 error C2144: syntax error : 'int' should be preceded by ';' u:\visual studio 2008\projects\grid\grid\grid.cpp 6 grid

Error 4 error C2057: expected constant expression u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 5 error C2466: cannot allocate an array of constant size 0 u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 6 error C2057: expected constant expression u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 7 error C2466: cannot allocate an array of constant size 0 u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 8 error C2087: 'grid' : missing subscript u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 9 error C2133: 'grid' : unknown size u:\visual studio 2008\projects\grid\grid\grid.cpp 46 grid

Error 10 error C2059: syntax error : ';' u:\visual studio 2008\projects\grid\grid\grid.cpp 48 grid












Let's look at this code first:
1
2
3
char grid [hieght][length];

grid [0][0]=*;


I'm afraid what you are attempting to do is probably a bit too advanced for you. You are attempting to dynamically create a multi dimensional array. For this to succeed you're going to first need to understand arrays and pointers or vectors. To start with, when declaring an array, the elements of the array must be a constant variable, which is a variable whose value cannot be changed. For example:

1
2
3
4
const int rows = 5;
const int columns = 5;

char grid[rows][columns];


Also, in order to assign * to grid[0][0], you need to write it this way grid[0][0] = '*'; because the asterisk is a character.
so what your saying is I can't have a multi-dimensional array that the user can choose the size of?
Last edited on
I didn't say you couldn't, it just won't work the way you're doing it.
this is really hard stuff, can someone help me out?

#include <iostream>
#include <vector>

using namespace std;
using std::vector;

int main()
{
vector<vector<char> > grid;

int length;
int hieght;
int L;
int H;
int row;
int collom;
double per;
double w;
double u;
double p;


row = 0;
collom = 0;

cout << "How long do you want your grid? ";
cin >> length;

cout << "How tall do you want your grid? ";
cin >> hieght;

cout << endl;

cout << "What percentage of nodes do you want? ";
cin >> per;
cout << endl;

w = length*hieght;

cout << "Out of " << w << " grid markers, you have ";
u = (per/100);
p = w * u;

int rounded = static_cast<int>(p+.5);

cout << rounded << " nodes." << endl;

hieght = hieght - 1;
length = length - 1;

H = hieght;

grid.resize(hieght);

for (int i = 0; i < hieght; ++i)
grid[i].resize(length);

do
{
L = length;
if( L > -1)
{
grid[H][L] = '*';
cout << grid[H][L];
L = L - 1;
}
H = H - 1;
}
while(H > -1);

cout << endl;

return 0;
}



It's not picking up any errors until I try to run it then it says

Debug assertion fail
line779
look
1
2
3
4
5
[.code]

//code goes HERE

[./code]

remove periods
Last edited on
I'm sorry, I don't understand?
When you are entering text into the forum, on the right side is a format section, click the # symbol and insert your code between the code tags. It formats it nicely and makes it considerably easier to read your code.
use code tags
when you post, look at the tool bar on the right,
click the # in the beginning before you start your code
then push it again when you are finished posting your code.
like this
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
#include <iostream>
#include <vector>

using namespace std;
using std::vector;

int main()
{
	  vector<vector<char> > grid;

	int length;
	int hieght;
	int L;
	int H;
	int row;
	int collom;
	double per;
	double w;
	double u;
	double p;
	

	row = 0;
	collom = 0;

	cout << "How long do you want your grid? ";
	cin  >> length;

	cout << "How tall do you want your grid? ";
	cin  >> hieght;

	cout << endl;

	cout << "What percentage of nodes do you want? ";
	cin  >> per;
	cout << endl;

	w = length*hieght;

	cout << "Out of " << w << " grid markers, you have ";
	u = (per/100);
	p = w * u;
	
	int rounded = static_cast<int>(p+.5);

	cout << rounded << " nodes." << endl;

	hieght = hieght - 1;
	length = length - 1;

	H = hieght;

	grid.resize(hieght);

	for (int i = 0; i < hieght; ++i)
	grid[i].resize(length);

	do 
	{
		L = length;
		if( L > -1)
		{
			grid[H][L] = '*';
			cout << grid[H][L];
			L = L - 1;
		}
		H = H - 1;
	}
	while(H > -1);

	cout << endl;

	return 0;
}


I still need my question answered
What were you trying to accomplish with this code? This is where the first error is.
1
2
for (int i = 0; i < hieght; ++i)
grid[i].resize(length);


Also, starting with your if statement
1
2
L = length;
      if( L > -1)

This is going to thrown an error when you get to it because L was never initialized to anything. It won't be able to compare L to -1.
closed account (z05DSL3A)
I still need my question answered

but now we can read your code easier.

Line 63, your access to grid looks like it is going out of bounds, you may want to move lines 48, 49 to between the resize and your do...while.
your error is here,
1
2
	for (int i = 0; i < hieght; ++i)
	grid[i].resize(length);

If the purpose is just to resize the vecort towards the lenght, then loose the loop.
your if statement is ok .
I'm trying to make a grid where each grid point can be assigned a (x,y) value and changed from a to b at the users will, then some other steps come after that, but I think I can do those pretty easily.
closed account (z05DSL3A)
try:
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
#include <iostream>
#include <vector>

using namespace std;
using std::vector;

int main()
{
	  vector<vector<char> > grid;

	int length;
	int hieght;
	int L;
	int H;
	int row;
	int collom;
	double per;
	double w;
	double u;
	double p;
	

	row = 0;
	collom = 0;

	cout << "How long do you want your grid? ";
	cin  >> length;

	cout << "How tall do you want your grid? ";
	cin  >> hieght;

	cout << endl;

	cout << "What percentage of nodes do you want? ";
	cin  >> per;
	cout << endl;

	w = length*hieght;

	cout << "Out of " << w << " grid markers, you have ";
	u = (per/100);
	p = w * u;
	
	int rounded = static_cast<int>(p+.5);

	cout << rounded << " nodes." << endl;

    grid.resize(hieght);

	for (int i = 0; i < hieght; ++i)
	grid[i].resize(length);

	hieght = hieght - 1;
	length = length - 1;

	H = hieght;

	do 
	{
		L = length;
		if( L > -1)
		{
			grid[H][L] = '*';
			cout << grid[H][L];
			L = L - 1;
		}
		H = H - 1;
	}
	while(H > -1);

	cout << endl;

	return 0;
}
Pages: 12