problems with arrays

What I thought this was going to do was put numbers in lineArray and rowArray.
lineArray[0] would be 1 and lineArray[1] would be 2 etc. that's not the output I'm getting and don't know why?

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
#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
#include <fstream>
#include <sstream>
#include <cstddef>
#include <iomanip>

using namespace std;

int main()
{
	const int MAX_ROW = 50;
	const int MAX_LINE = 50;
	const int MAX_MINE = 128;
	
	string lineArray[MAX_LINE];
	string rowArray[MAX_ROW];
	string mineArray[MAX_MINE];
	
	for (int i=0; i<54; i++)
	{
		rowArray[i] = i;
		cout << rowArray[i] << endl;
	}
	for (int i=0; i<24; i++)
	{
		lineArray[i] = i;
		cout << lineArray[i] << endl;
	}
	
	//cout << lineArray[1] << " Hi " << rowArray[1];
}
Last edited on
Both for-loops start with i=1, so how could the first element of the arrays ever be accessed?

Line 29: that variable is reset to 0 at the beginning of each loop, so it is always at 1 by the time it gets to line 31.
Last edited on
Yeah my bad I was testing some stuff and I see what your saying but its unimportant. So the first element with I=1 is also unimportant. I just want 1-54 in some postions I don't really care if it starts at 1 or 1001 as long as I got 1,2,3etc. Right now I don't have anything close to that.
The way the code is now after I edited it it should work right?
Last edited on
Just another dead question with no help.
closed account (48T7M4Gy)
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
#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
#include <fstream>
#include <sstream>
#include <cstddef>
#include <iomanip>

using namespace std;

int main()
{
	const int MAX_ROW = 50;
	const int MAX_LINE = 50;
	const int MAX_MINE = 128;
	
	int lineArray[MAX_LINE];          // <--
	int rowArray[MAX_ROW];            // <--
	int mineArray[MAX_MINE];          // <--
	
	for (int i=0; i<MAX_ROW; i++)      // <--
	{
		rowArray[i] = i;
		cout << rowArray[i] << endl;
	}
	for (int i=0; i<MAX_LINE; i++)    // <--
	{
		lineArray[i] = i;
		cout << lineArray[i] << endl;
	}
	
	//cout << lineArray[1] << " Hi " << rowArray[1];
}
jsonlickliter wrote:
Just another dead question with no help.
You don't need to report my post as a violation of the forum rules just because I was too busy to respond to you within a timely manner.
Oh sorry about that
Kemort that didn't work. I still get the same output?
I think ill just give up on this and learn vectors. They seem easier to me.
I think reporting someone for not replying to one of your threads fast enough is a pretty good way of alienating yourself on this forum.
I reported cause his answer left me stuck with no help. It didn't have to do with time.

And I don't want this issue to turn into cring and fighting. If its a problem or you don't agree then do something about it.
Last edited on
Kemort that didn't work. I still get the same output?

What doesn't work about kenort's code? I ran his code and it works fine, assuming you want the value of i in array[i].

if you want 1 in array[0], then simply add 1 to i when you store it.
 
		rowArray[i] = i + 1;  // Store i+1 into array[i] 


If that's not what you want, you going to have to be specific about what you're looking for.
I got this as output.

well I can't copy paste mu output and I have done that before.

I get blank lines and then it shows some charaters like !?,"':;()-/@123450 all on seperate line??

I think it should work too but I don't know what's wrong. Ill post the whole program.

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
#include <fstream>
#include <sstream>
#include <cstddef>
#include <iomanip>
//#include <vector>

using namespace std;

//functions

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	
	int line = 25;
	int row = 54;
	int print = 0;
	int print1 = 0;
	int print2 = 0;
	int print3 = 0;
	int mines = 432;
	int lineData = 0;
	int rowData = 0;
	
	string mapStar = "*";
	string mapPlayer = "#";
	string mapRow = "******************************************************";
	string userInput;
	
	const int MAX_ROW = 60;
	const int MAX_LINE = 60;
	const int MAX_MINE = 128;
	
	string lineArray[MAX_LINE];
	string rowArray[MAX_ROW];
	string mineArray[MAX_MINE];
	
	for (int i=0; i<MAX_ROW; i++)
	{
		rowArray[i] = i;
		cout << rowArray[i] << endl;
	}
	for (int i=0; i<MAX_LINE; i++)
	{
		lineArray[i] = i;
		cout << lineArray[i] << endl;
	}
	
	cout << lineArray[1] << " Hi " << rowArray[1];
	cout << string( 100, '\n' );
	for (int i=0; i<24; i++)
	{
		cout << mapRow << endl;
	}
	cout << "*****************************************************#" << endl;
	cout << endl;
	do
	{
		cout << "[" << line << "," << row << "] ";
		cin >> userInput;
		if (userInput == "u")
		{
			cout << string( 100, '\n' );
			line = line -1;
			print = line -1;
			for (int i=0; i<print; i++)
			{
				cout << mapRow << endl;
			}
			//print row
			print2 = row -1;
			for (int i=0; i<print2; i++)
			{
				cout << mapStar;
			}
			cout << mapPlayer;
			print3 = 53 - print2;
			for (int i=0; i<print3; i++)
			{
				cout << mapStar;
			}
			cout << endl;
			print1 = 24 - print;
			for (int i=0; i<print1; i++)
			{
				cout << mapRow << endl;
			}
	
			if (line < 1)
			{
				cout << endl;
				cout << "You can't go up any more." << endl;
				line = 1;
			}
		}
		if (userInput == "d")
		{
			cout << string( 100, '\n' );
			line = line +1;
			print = line -1;
			for (int i=0; i<print; i++)
			{
				cout << mapRow << endl;
			}
			//print row
			print2 = row -1;
			for (int i=0; i<print2; i++)
			{
				cout << mapStar;
			}
			cout << mapPlayer;
			print3 = 53 - print2;
			for (int i=0; i<print3; i++)
			{
				cout << mapStar;
			}
			cout << endl;
			print1 = 24 - print;
			for (int i=0; i<print1; i++)
			{
				cout << mapRow << endl;
			}
			if (line > 25)
			{
				cout << "You can't go down any more." << endl;
				line = 25;
			}
		}
		if (userInput == "l")
		{
			print = line -1;
			cout << string( 100, '\n' );
			row = row -1;
			print2 = row -1;
			print1 = 24 - print;
			for (int i=0; i<print; i++)
			{
				cout << mapRow << endl;
			}
			for (int i=0; i<print2; i++)
			{
				cout << mapStar;
			}
			cout << mapPlayer;
			print3 = 53 - print2;
			for (int i=0; i<print3; i++)
			{
				cout << mapStar;
			}
			cout << endl;
			for (int i=0; i<print1; i++)
			{
				cout << mapRow << endl;
			}
			if (row < 1)
			{
				cout << "You can't go left any more." << endl;
				row = 1;
			}
		}
		if (userInput == "r")
		{
			print = line -1;
			cout << string( 100, '\n' );
			row = row +1;
			print2 = row -1;
			print1 = 24 - print;
			for (int i=0; i<print; i++)
			{
				cout << mapRow << endl;
			}
			for (int i=0; i<print2; i++)
			{
				cout << mapStar;
			}
			cout << mapPlayer;
			print3 = 53 - print2;
			for (int i=0; i<print3; i++)
			{
				cout << mapStar;
			}
			cout << endl;
			for (int i=0; i<print1; i++)
			{
				cout << mapRow << endl;
			}
			if (row > 54)
			{
				cout << "You can't go right any more." << endl;
				row = 54;
			}
		}
	} while (userInput != "quit");
}
I think the problem here is I declared a string.

string lineArray[MAX_LINE]; on line 39

And then in the for loop i is an int

for (int i=0; i<MAX_LINE; i++)
{
lineArray[i] = i;
cout << lineArray[i] << endl;
}

So can I put an int data number in a string array?
Now this works right but ill have to change MAX_ROW to 54 and of course it puts 54 i's in each postion.

for (int i=0; i<MAX_ROW; i++)
{
rowArray[i] = "i";
cout << rowArray[i] << endl;
}

So it looks to me I need to convert the data number to string then put it into the rowArray[I] so I get

rowArray[0] = 1
RowArray[1] = 2

Etc...

Then later when I need to do some math I have to convert the string numbers back to data numbers.

So "What doesn't work about kenort's code? I ran his code and it works fine, assuming you want the value of i in array[i]."

How did that work for you again? Is there something you got I don't have?
Oh I also tried this

for (int i=0; i<MAX_ROW; i++)
{
rowArray[i] = 4;
cout << rowArray[i] << endl;
}

It gave me a bunch of blank lines.
for (int i=0; i<55; i++)
{
string String = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
rowArray[i] = String;
cout << rowArray[i] << endl;
}

I wonder what this dose?

If you like to learn how to convert it back to data and actually learn something I can tell you.
closed account (48T7M4Gy)
ASCII characters are integers. That is why you are getting blanks and what appears to be random characters because the integer 1 is not ASCII '1'. See http://www.cplusplus.com/doc/ascii/?kw=ASCII among others.

If you want to store a number as a string then see http://www.cplusplus.com/reference/string/to_string/
Topic archived. No new replies allowed.