• Advertisement
Sign in to follow this  

Repeating line problem

This topic is 4382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: This has been updated, thanks Sailorstick for helping with the first part. I have some code to create a text based map which has a bug in it. The bug is it repeats the first line of the map,
	int maparray[] = { 0,11,9,10,8 };
	int length = 4;
	ROOM* map1 = new ROOM[length];
	for (int i = 1;i < (length+1);i++)
	{
		map1[i-1].CreateMapRoom(maparray[i],i,i,1);
	}
	int maxy = 2;
	int maxx = 2;
	// Room Lines
	string rooma = "";
	string roomb = "";
	string roomc = "";
	// Maplines
	string mapline1 = "";
	string mapline2 = "";
	string mapline3 = "";
	// Counter
	int count = 0;

	for (int yco = 1;yco < maxy+1; yco++)
	{
		for (int xco = 1; xco < maxx+1; xco++) 
		{

			switch(map1[count].GetType())
			{
			case 1:
				{
					// Top and bottom
					rooma = "+- -+";
					roomb = "|   |";
					roomc = "+- -+";
					break;
				}
			case 2:
				{
					// Bottom
					rooma = "+---+";
					roomb = "|   |";
					roomc = "+- -+";
					break;
				}
			case 3:
				{
					// Top
					rooma = "+- -+";
					roomb = "|   |";
					roomc = "+---+";
					break;
				}
			case 4:
				{
					// Left
					rooma = "+---+";
					roomb = "    |";
					roomc = "+---+";
					break;
				}
			case 5:
				{
					// Right
					rooma = "+---+";
					roomb = "|    ";
					roomc = "+---+";
					break;
				}
			case 6:
				{
					// Left and Right
					rooma = "+---+";
					roomb = "     ";
					roomc = "+---+";
					break;
				}
			case 7:
				{
					// All 4
					rooma = "+- -+";
					roomb = "     ";
					roomc = "+- -+";
					break;
				}
			case 8:
				{
					// Top and Left
					rooma = "+- -+";
					roomb = "    |";
					roomc = "+---+";
					break;
				}
			case 9:
				{
					// Bottom and Left
					rooma = "+---+";
					roomb = "    |";
					roomc = "+- -+";
					break;
				}
			case 10:
				{
					// Top and Right
					rooma = "+- -+";
					roomb = "|    ";
					roomc = "+---+";
					break;
				}
			case 11:
				{
					// Bottom and Right
					rooma = "+---+";
					roomb = "|    ";
					roomc = "+- -+";
					break;
				}
			case 12:
				{
					// Top, Bottom and Right
					rooma = "+- -+";
					roomb = "|    ";
					roomc = "+- -+";
					break;
				}
			case 13:
				{
					// Top, Bottom and Left
					rooma = "+- -+";
					roomb = "    |";
					roomc = "+- -+";
					break;
				}
			case 14:
				{
					// Top, Left and Right
					rooma = "+- -+";
					roomb = "     ";
					roomc = "+---+";
					break;
				}
			case 15:
				{
					// Bottom, Left and Right
					rooma = "+---+";
					roomb = "     ";
					roomc = "+- -+";
					break;
				}
			case 16:
				{
					// Blank
					rooma = "     ";
					roomb = "     ";
					roomc = "     ";
					break;
				}
			case 17:
				{
					// Left Wall
					rooma = "+    ";
					roomb = "|    ";
					roomc = "+    ";
					break;
				}
			case 18:
				{
					// Right Wall
					rooma = "    +";
					roomb = "    |";
					roomc = "    +";
					break;
				}
			case 19:
				{
					// Top Wall
					rooma = "+---+";
					roomb = "     ";
					roomc = "     ";
					break;
				}
			case 20:
				{
					// Bottom Wall
					rooma = "     ";
					roomb = "     ";
					roomc = "+---+";
					break;
				}
			default:
				break;
			}
			if (count < length)
				count++;

			mapline1 += rooma;			
			mapline2 += roomb;			
			mapline3 += roomc;

		}
		cout << mapline1 << "\n";
		cout << mapline2 << "\n";
		cout << mapline3 << "\n";
	}
[Edited by - Iccarus on January 19, 2006 9:57:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
Whoah, you got the sizes and indexes all wrong...

int maparray[] = { 0,11,9,10,8 };
int length = 4;
ROOM* map1 = new ROOM[length];
for (int i = 1;i < (length+1);i++)
{
map1[i].CreateMapRoom(maparray[i],i,i,1);
}




Should be:

int maparray[] = { 11,9,10,8 };
int length = 4;
ROOM* map1 = new ROOM[length];
for (int i = 0;i < length; i++)
{
map1[i].CreateMapRoom(maparray[i],i,i,1);
}


Share this post


Link to post
Share on other sites
It may be this line of code


for (int i = 1;i < (length+1);i++)
{
map1[i].CreateMapRoom(maparray[i],i,i,1);
}


Array indices start at 0, not 1. Try going from 0 to "i < length".

[edit] Beaten

Share this post


Link to post
Share on other sites
I'm doing that on purpose so it ignores the first array place. I know it goes from 0 but I don't want it to read out 0, so I'm starting from 1.

Would that cause the problem?

Share this post


Link to post
Share on other sites
Well, technically there shouldn't be a problem doing that, but it would probably less confusing if you didn't.

The problem is because your count++ before accessing the map1[count], so on the last loop count==4 and map1[4] does not exist.
Put your count++ after your switch

Share this post


Link to post
Share on other sites
count++;
switch(map1[count].GetType())

Are you sure that count is ok? I mean u sure that it isn't exceeding the range of the array? I mean u have a loop maxy on maxx size, and you never reset the count so it is bound to exceed the range of the array...

EDIT: Ussualy access violations are some stuff that has to do with pointers and stuff...

Share this post


Link to post
Share on other sites
Also need to fix this
for (int i = 1;i < (length+1);i++)
{
map1[i-1].CreateMapRoom(maparray[i],i,i,1);
}

use i-1 for the map1 index if you are going to go from 1 to length+1.

Share this post


Link to post
Share on other sites
Cheers thats the debug problem fixed, now I need to understand why its printing the first line twice but without a newline the secondtime.

Share this post


Link to post
Share on other sites
cout << mapline1 << "\n";

it prints this one twice?
or this one and the next one without a new line?
did u try maybe endln instead of "\n"

EDIT: or try for example:

mapline3 += (roomc + "\n");

Share this post


Link to post
Share on other sites
Never mind, I'm an idiot.

Its because I forgot to reset the string array, its simply adding the next stuff on to it because of that.

Thank you both for you help :D

Share this post


Link to post
Share on other sites
I don't know if this will help or not, but sometimes I get these errors with file output and I need to explicity call flush on the output stream. Try flushing cout inside the loop after the last cout.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement