Problem with 2d char array output

Hello, I am having trouble with displaying my 2d char array. I am initially getting it from a text file. beginGame is a function that is defined outside of main, but it works. Here are the two files. I am getting the exact same amount of characters as a line in the Map.txt. I am not for sure if I have something odd stored in the the array map, or if I have done something that messed up the null terminators in the array.

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
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <cmath>
using namespace std;

// prototypes for our functions

int main()
{
	int dragonHealth, dragonAttack, height;
	int rangerHealth, rangerAttack, sorcererHealth, sorcererAttack, fighterHealth, fighterAttack;
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\tDRAGON'S FALL ";
	cout << "\n\n\n\nInstructions: You will be guided through the game with menus, and \n";
	cout << "Your goal is to defeat the dragon. You have three classes on \n";
	cout << "the field. Ranger, sorcerer, and fighter. You will pick any\n";
	cout << "placement of the three and fight your way to the dragon. \n";
	cout << "\nPress enter to continue\n";
	cin.get();
	cout << "Goblins are represented with R, Elementals are represented with E,\n";
	cout << "and the dragon is represented with D. Items are represented with I,\n";
	cout << "trees are represented with T, water is represented with W, and cliffs\n";
	cout << "are represented with C. Also, your characters are represented by the first\n";
	cout << "letter of their class. \n";
	cout << "\nPress enter to continue\n";
	cin.get();
	beginGame(height);
	string line;
	char map[height][43];
	int curLine;
	ifstream mapFile;
	/* I begin by opening my file and then determining what line I am on. If I am in the first 2 lines, then I display
	   the first 2 lines of my text file. If not then I am pulling a random line from the text file.
	*/
	for (int I =0; I < height; I++)
		{
			mapFile.open("Map.txt");
			if (I == 0 )
				{
				}
			else if ( I == 1)
				{
					//ignores the first line to get the second one
					getline(mapFile, line);
				}
			else 
				{
					// gets random number and verifies it is within the files boundaries
					curLine = (rand() % 33);
					while (curLine == 0 || curLine == 1)
						{
							curLine = rand() % 33;
						}
					// skips us to the random line in the file
					for (int X = 0; X < curLine; X++)
						{
							getline(mapFile, line);
						}
				}
			mapFile.getline (map[I],42);
			// by closing and reopening we start back at the beginning of the file
			mapFile.close();
		}
	// displays the map this is where I am having problems
	for (int I =0; I < height; I++)
		{
			for (int X =0; X < 43; X++)
			{
				cout << map[I][X];
			}
			cout << "\n";
		}
	return 0;
}




and here is Map.txt

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
|                    D                  |
|     I       I           I       I     |
|           I        I         I        |
|              I            I           |
|                    I                  |
|     G              G             G    |
|           G                 G         |
|                    G                  |
|      E             E           E      |
|            E              E           |
|                    E                  |
|TTTTTTTTTTTTT                          |
|TTTTTTTTTTTTTTTTTTTTTTTTTTT            |
|TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT|
|TTTTTTTTTTTTT       E             E    |
|TTTTTTTTTTTTT       E             G    |
|TTTTTTTTTTTTT       G             G    |
|TTTTTTTTTTTTT               E          |
|TTTTTTTTTTTTT               G          |
|TTTTTTTTTTTTT               I          |
|TTTTTTTTTTTTTTTTTTTTTTTTTTT      E     |
|TTTTTTTTTTTTTTTTTTTTTTTTTTT      G     |
|TTTTTTTTTTTTTTTTTTTTTTTTTTT      I     |
|   E               TTTTTTTTTTTTTTTTTTTT|
|   G               TTTTTTTTTTTTTTTTTTTT|
|   E         E     TTTTTTTTTTTTTTTTTTTT|
|   E         G     TTTTTTTTTTTTTTTTTTTT|
|   G         E     TTTTTTTTTTTTTTTTTTTT|
|   G         G     TTTTTTTTTTTTTTTTTTTT|
|   G        TTTTTTTTTTTTTTTTTTTTTTTTTTT|
|   E        TTTTTTTTTTTTTTTTTTTTTTTTTTT|
|   I        TTTTTTTTTTTTTTTTTTTTTTTTTTT|





And the output will be something like
-----------------------------------------
|                    D                  |
|     I       I           I       I     |  @
|   E         G     TTTTTTTTTTTTTTTTTTTT|  X
|   E         E     TTTTTTTTTTTTTTTTTTTT|  !
|            E              E           |
|                    I                  |  *
|     G              G             G    |  +


and so on
the desired out put should not have these weird characters.
Last edited on
Soved this by cutting down my array size by one. So instead of 43 it is 42.
Topic archived. No new replies allowed.