#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;



const int numRows = 22;  // Number of rows in the board
const int numColumns = 80; // Number of columns in the board
int row;
int column;
char Drawing[numRows][numColumns]; // environment representing all the cells and their state.
char point[6];




void initData();
void getPoint();
void FillDrawing(char Drawing[numRows][numColumns], int row, int column);
void printArrayState(char myArray[numRows][numColumns]);






int main()
{
    initData();
    printArrayState(Drawing);
    cout << "Enter starting point in the format x,y: ";
    getPoint();
    void FillDrawing(char Drawing[numRows][numColumns], int row, int column);
    system("cls");
    printArrayState(Drawing);
    //system{"pause");
    return 0;
}


void getPoint()
{
    cin >> point;
    sscanf_s(point, "%02d,%02d", &row, &column);
    row = row-1;
    column = column-1;
}



void initData()
{
    char s, str[256];
    ifstream input;

    for(int x=0; x < numRows; x++)
            {
                for (int y=0; y < numColumns; y++)
                {
                      Drawing[x][y] = ' ';
                }
            }


    cout << "Enter the file name containing the drawing" << endl;
    cin >> str; 
    input.open(str);

    if(input.fail())
    {
        cout << "Input file not found" << endl;
        exit(1);
    }

    while(!input.eof())
    {
            for(int x=0; x < numRows && !input.eof() && x != '\n'; x++)
            {
                for (int y=0; y < numColumns && !input.eof(); y++)
                {
                      s = input.get();
                      Drawing[x][y] = s;   
                }
            }
    }



    input.close();
    system("cls");

}


void printArrayState(char myArray[numRows][numColumns])
{
    for (int i = 0; i < numRows && i != '\n'; i++)
    {
        for (int j = 0; j < numColumns; j++)
        {
            cout << myArray[i][j];
        }
    }
    cout << endl;
}


void FillDrawing(char myArray[numRows][numColumns], int row, int column)
{

    if (row < 0 || row > numRows || column > numColumns || column < 0 || myArray[row][column] == '*' || myArray[row][column] == '#') 
     {    
         return;
     }

     else
     {
    char star = '*';
     myArray[row][column] = star;

        FillDrawing(myArray,row+1,column);//down
        FillDrawing(myArray,row-1,column);//up
        FillDrawing(myArray,row,column+1);//left
        FillDrawing(myArray,row,column-1);//right
     }
}

this is the code to my program. i am to read in a file that has a shape made of * and put the shape into a 2d array. The uses inputs a starting position and i am to fill the shape with * until i hit a wall. my program is not filling in the shape. after reviewing other threads on other sites, I think it's the way i am storing my array when i read it from the file. However, at this point i'm completely lost. thanks for the help in advance!

Recommended Answers

All 15 Replies

This is actually a pretty clever problem. Can you be sure that your input file is exactly 22x80? Were you given any other rules about the contents of the input file?

yes. the input file will never go over 22x80. my professor told me to change my init functions so that my for loop is now:

for(int x=0; x < numRows && !input.eof(); x++)
		{
            for (int y=0; y < numColumns && !input.eof() && s!= '\n'; y++)
		        {
			        s = input.get();
                    Drawing[x][y] = s;   
		        }
            s = input.get();
        }

however, my house now prints to the far right of the screen, so i know it's not going to fill correctly.

here's the assignment itself: http://www.cs.ecu.edu/rhoggard/3300/assignments/assg03.htm

can you attach the txt file that contains sample shape

here you go!

There was some issues so bear with me
1-initData:

for(int x=0; x < numRows && !input.eof() && x != '\n'; x++)

the condition " x != '\n'" is wrong since it means if x==10 so it will read 10 lines so make it

for(int x=0; x < numRows && !input.eof() ; x++)

and add this :

if(s=='\n')
    break;

after :

s = input.get();

2-printArrayState:
Same first error

for (int i = 0; i < numRows && i != '\n'; i++)

change to:

for (int i = 0; i < numRows ; i++)

3-main:
You are declaring the function??

void FillDrawing(char Drawing[numRows][numColumns], int row, int column);

no you need to call it:

FillDrawing(Drawing, row, column);

thanks mazzica1....my professor caught the first 4 errors you mentioned so those have been taken care of. i completely overlooked the fact that i was declaring my fill function again, so thanks for that too.

now i'm trying to figure out why my drawing is printing all the way to the right.

it is not to the right the file has 12 space so that make it seems to the right

oh. if you run my prof's demo program, his doesn't do that. how do i make it print in the middle?

You will need to adjust the Shape in the array calculate min left * and max right * then subtract from the width and devide the result by 2 then subtract the min left will give you the number of spaces to add
i think i am not clear so:

spacesToAdd=((width-(maxRightStarInXDir-minLeftStarInXDir))/2)-minLeftStarInXDir;

ofcourse you will need to check

if(spacesToAdd>0)

whoooooooooooa! you just lost me! lol

what do i do with spacesToAdd?

let me say it like this:
1- getMinLeftStarXDir

int getMinLeftStarXDir(char area[numRows][numColumns])
{
	int res=-1;
	for(int i=0;i<numColumns;i++)
	{
		for(int j=0;j<numRows;j++)
		{
			if(area[j][i]=='*')
			{
				res=i;
				break;
			}
		}
		if(res!=-1)
		{
			break;
		}
	}
	return res;
}

1- getMaxRightStarXDir

int getMaxRightStarXDir(char area[numRows][numColumns])
{
	int res=-1;
	for(int i=numColumns-1;i>-1;i--)
	{
		for(int j=numRows-1;j>-1;j--)
		{
			if(area[j][i]=='*')
			{
				res=i;
				break;
			}
		}
		if(res!=-1)
		{
			break;
		}
	}
	return res;
}

3- in printAreaState:
3.1:
add at the begining:

int spacesToAdd=((numColumns-(getMaxRightStarXDir(myArray)-getMinLeftStarXDir(myArray)))/2)-getMinLeftStarXDir(myArray);

3.2:
add and change he inner loop:

int j = 0;
for(;j<spacesToAdd;j++)
{
	cout<<" ";
}
for (; j < numColumns; j++)
{
	cout << myArray[i][j-spacesToAdd];
}

ok but i want you to notice these:
1-the fill function has some error:

if (row < 0 || row > numRows || column > numColumns || column < 0 || myArray[row][column] == '*' || myArray[row][column] == '#')

it need to check if row>numRows-1 cause if you selected 1,1 to fill will fill wrong area
and same for column so:

if (row < 0 || row > numRows-1 || column > numColumns-1 || column < 0 || myArray[row][column] == '*' || myArray[row][column] == '#')

2-if you used the middling functions i told you about this will manipulate the out only so if you clicked 1,1 this will not center the shape to solve this you will need to move the shape in the array not just in the display wich could cause some problem if the prof. enterd Eg:11,17 as in window in original it will not fill it so it is up to you !!

i see. thanks for all your help mazzica1!

Your welcome !!
and always have fun with programming :) :)

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.