I am trying to calculate the height and width of a char array? The char is like an Ascii art of a card. And what I am trying to do is calculate how long the width is before the '\n'(newline), and how long the height is till the null terminator. Thanks.

you can use a loop to check for '\n',run it till you find it and count that many times.you will get its size.

you can use a loop to check for '\n',run it till you find it and count that many times.you will get its size.

Ok here is what I got to try and calculate the width and height.

for(int i=0; i<size; ++i)
	{
		if(text[i] == '\n')
			height += 1;
	}

for(int i=0; i < size; ++i)
	if(text[i] != '\n')
		width += 1;

But, whenever I run the program the width is wrong and the height is 0. I know that it is something with my code, but I can't seem to figure it out. Thanks

for(int i=0; i<size; ++i)
	{
		if(text[i] == '\n')
			height += 1;
	}

If there are \n characters in your text array, this loop will count them. Is height initialized properly?

for(int i=0; i < size; ++i)
	if(text[i] != '\n')
		width += 1;

All this does is count the number of non-\n characters in the buffer. Should be size - height

I don't really understand if its a 1-D array or a 2-D array.
if its a 1-D array like this one then this should work:

char arr[]= {"this is the first line\n this is the second\n and this is the third line"};

   int width=0,height=1,size=0;

   size = strlen(arr);

   for(int i=0;i<size;i++)
   {
		if(arr[i]=='\n')	//mean we encounter a new line
		{
			height++;		//then increment height
		}
		else				//otherwise the width increases
		{
			width++;
		}
   }


   
   
   cout << height << endl << width << endl;

Note that the width is the sum of the total non-'\n' characters in the string,if you want the width of each line separately then copy the value of the width to some other variable when you get '\n' and set width to zero for calculating the width of the next line.

You're welcome.

commented: We don;t solve problems for people -- we point out how to solve them for themselves. -3
for(int i=0; i<size; ++i)
	{
		if(text[i] == '\n')
			height += 1;
	}

If there are \n characters in your text array, this loop will count them. Is height initialized properly?

for(int i=0; i < size; ++i)
	if(text[i] != '\n')
		width += 1;

All this does is count the number of non-\n characters in the buffer. Should be size - height

I don't really understand if its a 1-D array or a 2-D array.
if its a 1-D array like this one then this should work:

char arr[]= {"this is the first line\n this is the second\n and this is the third line"};

   int width=0,height=1,size=0;

   size = strlen(arr);

   for(int i=0;i<size;i++)
   {
		if(arr[i]=='\n')	//mean we encounter a new line
		{
			height++;		//then increment height
		}
		else				//otherwise the width increases
		{
			width++;
		}
   }


   
   
   cout << height << endl << width << endl;

Note that the width is the sum of the total non-'\n' characters in the string,if you want the width of each line separately then copy the value of the width to some other variable when you get '\n' and set width to zero for calculating the width of the next line.

You're welcome.

Thanks a lot both of you. It helped me figured out what was wrong with my code. Again Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.