Why does this code work so weirdly?

It doesn't make sense at all. Here's the code:

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <string>


using namespace std;

int main() {
string mark[936], block="=", player="G";
int i=0;

for (i=0;i<72;i=i+1)
mark[i]=block;
for (i=864;i<936;i=i+1)
mark[i]=block;
mark[431]=player;

for (i=0;i<=71;i=i+1)
cout << mark[i];
cout << endl;
for (i=72;i<=143;i=i+1)
cout << mark[i];
cout << endl;
for (i=144;i<=215;i=i+1)
cout << mark[i];
cout << endl;
for (i=216;i<=287;i=i+1)
cout << mark[i];
cout << endl;
for (i=288;i<=359;i=i+1)
cout << mark[i];
cout << endl;
for (i=360;i<=431;i=i+1)
cout << mark[i];
cout << endl;
for (i=432;i<=503;i=i+1)
cout << mark[i];
cout << endl;
for (i=504;i<=575;i=i+1)
cout << mark[i];
cout << endl;
for (i=576;i<=647;i=i+1)
cout << mark[i];
cout << endl;
for (i=648;i<=719;i=i+1)
cout << mark[i];
cout << endl;
for (i=720;i<=791;i=i+1)
cout << mark[i];
cout << endl;
for (i=792;i<=863;i=i+1)
cout << mark[i];
cout << endl;
for (i=864;i<=935;i=i+1)
cout << mark[i];

return 0;
}

The "G" always appears on the first spot of a line, no matter what number I type on the line "mark[431]=player;". Try changing the number and see that the G is always on the first spot of a line.

EDIT:
Ohh. I've fixed it, I simply forgot to fill strings with spaces. I've also made the code shorter. Here it is:

#include <iostream>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <string>


using namespace std;

int main() {
string mark[936], block="=", player="G", empty=" ";
int i=0,j=0;

for (i=0;i<936;i=i+1)
mark[i].assign(empty);

for (i=0;i<72;i=i+1)
mark[i].assign(block);
for (i=864;i<936;i=i+1)
mark[i].assign(block);
mark[431].assign(player);

for (j=0;j<=864;j=j+72) {
if (j)
cout << endl;
for (i=j;i<=j+71;i=i+1)
cout << mark[i]; }
return 0;
}


EDIT2:
And that string.assign(45345); is the same as string = 45345;


Oh. I've always created games using consoles. Then I think I should start creating an Android app or something like that, though I do actually like creating console games.


I think if I learned using ncurses, I could create really great games. Then the screen could easily get refreshed without lag and therefore it could be refreshed every like 10 ms. I hope that wouldn't make any lag. For ex., imagine the snake game. The console would need to refresh a lot but hopefully if the ncurses are used it wouldn't lag and a great game could be created. So I'm thinking of creating console games using ncurses, though I haven't yet started using them and I don't know how.
Last edited on
If you're going to post uncommented code and not use code tags... then at least tell us what the code is supposed to be doing.

EDIT: okay I see what you're trying to do now... hang on...

EDIT2:

strings default to an empty string. If you output an empty string, it will print nothing.

So you are printing this:

 
<nothing><nothing><nothing>G<nothing>


Which looks like this:
 
G



But what you seem to want is this:
 
<space><space><space>G<space>


which would look like this:
 
   G 



That said.... use another for loop. One for the rows and one for the columns. Copy/pasting code numerous times like that is a sign that you are doing something very wrong. Also, try to avoid "magic numbers"... numbers like 647 and 935 are not very clear as to what they do or mean, and it's very easy to get them wrong.


EDIT AGAIN:

Also if you are only outputting a single character, you can use a char instead of a string. Strings are for groups of multiple characters.



Lastly... if you're going to make a game, don't use the console.
Last edited on
Well, all the elements of mark[] are initialized to the empty string, so any elements that appear before the G won't make a difference.
Topic archived. No new replies allowed.