What is wrong with my code? It repeats the "Enter the next row:" statement more than once for each time its executed. And the characters turn in to brackets.

/*
* File: fill.cpp
* -----------------
* This program gets the perimeter of a closed shape
* as an input and fills in the area of the shape.
*/

#include <stdio.h>
#include "simpio.h"

#define size 3

void fill(int row, int column,char shape[size][size]);

int main()
{
	char c;
	char shape[size][size];
	int m,n;
	printf("This program gets the perimeter of a closed shape\n");
	printf("as an input and fills in the area of the shape.\n");
	printf("Enter the perimeter of the shape row by row.\n");
	printf("Use '*' for the perimeter and ' ' for the inside of the shape.\n");
	printf("Enter the first row: ");
	for(m=0; m<=size-1; m++)
	{
		n=0;
		while(n<=size-1)
		{
			c=getchar();
			shape[m][n]=c;
			printf("Enter the next row: ");
			n++;
			
		}
		
	}
}

/****************************************fill**************************************/
/* This program fills in the area of the shape using a recursive function. */

void fill(int row,int column,char shape[size][size])
{
	int m,n;
	m=row;
	n=column;
	if(shape[m][n]!='*')
	{
		shape[m][n]='*';
		fill(m,n+1,shape);
		fill(m,n-1,shape);
		fill(m-1,n,shape);
		fill(m+1,n,shape);
	}
}

Your prompt to enter a row should be just inside the outer (m) loop, since that loop controls rows.

Also, normal style for for loops is to compare counter < limit, rather than your convoluted counter <= limit-1.

Why is your inner loop a while, instead of also being a for loop?

>>It repeats the "Enter the next row:" statement more than once for each time its executed.

Looks like it should be printed 9 times according to your code. Specifically when:
m = 0, n = 0
m = 0, n = 1
m = 0, n = 2
m = 1, n = 0, 1, 2
m = 2, n = 0, 1, 2

>>And the characters turn in to brackets

How can you tell. I don't see where you output the values stored in shape at all in your program. For that matter I don't see where fill() is used, nor can I tell why you bother including simpio. And on top of that the code is written in native C, not C++ so if you haven't posted there then you might conisder it.

Now the for loop only runs two times even though its set to run four times.

/*
* File: fill.cpp
* -----------------
* This program gets the perimeter of a closed shape
* as an input and fills in the area of the shape.
*/

#include <stdio.h>
#include "simpio.h"

#define size 4

void fill(int row, int column,char shape[size][size]);

int main()
{
	char c;
	char shape[size][size];
	int m,n;
	printf("This program gets the perimeter of a closed shape\n");
	printf("as an input and fills in the area of the shape.\n");
	printf("Enter the perimeter of the shape row by row.\n");
	printf("Use '*' for the perimeter and ' ' for the inside of the shape.\n");
	printf("Enter the first row: ");
	for(m=0; m<=size-1; m++)
	{
		
		for(n=0; n<size; n++)
		{
			c=getchar();
			shape[m][n]=c;
		}
		printf("Enter the next row: ");
		
	}
}

/****************************************fill**************************************/
/* This program fills in the area of the shape using a recursive function. */

void fill(int row,int column,char shape[size][size])
{
	int m,n;
	m=row;
	n=column;
	if(shape[m][n]!='*')
	{
		shape[m][n]='*';
		fill(m,n+1,shape);
		fill(m,n-1,shape);
		fill(m-1,n,shape);
		fill(m+1,n,shape);
	}
}

It runs 4 times for me. The prompt to enter another row is in a bad place, it comes up one more time after the last row has been entered.

Perhaps

// printf("Enter the first row: ");  //delete this line
	for(m=0; m<=size-1; m++)
	{
	        printf("Enter a row: ");
		for(n=0; n<size; n++)
		{
			c=getchar();
			shape[m][n]=c;
		}
	}

will give better prompting.

It runs 4 times for me. The prompt to enter another row is in a bad place, it comes up one more time after the last row has been entered.

Perhaps

// printf("Enter the first row: ");  //delete this line
	for(m=0; m<=size-1; m++)
	{
	        printf("Enter a row: ");
		for(n=0; n<size; n++)
		{
			c=getchar();
			shape[m][n]=c;
		}
	}

will give better prompting.

I meant the inner for loop doesn't run enough.

I meant the inner for loop doesn't run enough.

I was able to enter four rows of four characters. At least, I believe that's what it did, not seeing any display.

I was able to enter four rows of four characters. At least, I believe that's what it did, not seeing any display.

Yeah. but with a display you can see that it only allows you to enter two characters. I think it might have something to do with the variable n since it changes wierldy when I use printf to display its value.

No, it has to do with how you're getting the input. getchar() grabs every char entered, including the newline. You end with extraneous data in your array.

If you enter the 16 characters in one single line, then hit enter, like ***** ** ****** you'll have your framed box.

You're writing C++, why not use the C++ input method, which will skip over whitespace.

for(m=0; m<=size-1; m++)
	{
		for(n=0; n<size; n++)
		{
			cin >> shape[m][n];
		}
	}

No, it has to do with how you're getting the input. getchar() grabs every char entered, including the newline. You end with extraneous data in your array.

If you enter the 16 characters in one single line, then hit enter, like ***** ** ****** you'll have your framed box.

You're writing C++, why not use the C++ input method, which will skip over whitespace.

for(m=0; m<=size-1; m++)
	{
		for(n=0; n<size; n++)
		{
			cin >> shape[m][n];
		}
	}

How can I write this in C. Just wondering what the difference would be.

How can I write this in C. Just wondering what the difference would be.

Well, you're in the C++ forum, thought you'd be using C++.

In C, I suppose you can still use the getchar(), but you need to check that it's not the newline that's just been read.

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.