I am trying to create a program that uses nested loops to create a box. Only one of the overloaded functions need the details of nested loops.. the other 3 should call the one that has the details.. my problem is the last function and the for nested loops.

Here is what I have so far

***Code***

#include <iostream>
using namespace::std;

//  Function prototypes

void displayBox ( int length );
void displayBox ( int length, char fillChar );
void displayBox ( int width,  int height    );
void displayBox ( int width,  int height, char FillChar );

//--------------------------------------------------------------------

int main()
{
    int boxLength,   // Input box dimensions
        boxWidth,
        boxHeight;

    char boxFill;    // Input fill character

    // Test the displayBox(length) and displayBox(length,fillChar)
    // functions.
    cout << endl << "Enter the length of a side: ";
    cin >> boxLength;
    displayBox(boxLength);
    cout << endl << "Enter the fill character: ";
    cin >> boxFill;
    displayBox(boxLength,boxFill);

    // Test the displayBox(width,height) and
    // displayBox(width,height,fillChar) functions.
    cout << endl << "Enter the width and height of the box: ";
    cin >> boxWidth >> boxHeight;
    displayBox(boxWidth,boxHeight);
    cout << endl << "Enter the fill character: ";
    cin >> boxFill;
    displayBox(boxWidth,boxHeight,boxFill);

    return 0;
}

//--------------------------------------------------------------------
void displayBox ( int length )
{
   //  call the 2 agrument function, int and char
	char fill = " ";
	displayBox(length, fill);
}


void displayBox ( int length, char fillChar )
{
	//  call the 3 argument function3
	displayBox(length, length, fillChar);
}


void displayBox ( int width,  int height    )
{
	// call the 3 argument function
	char fil = " ";
	displayBox(width, height, fill);
}

void displayBox ( int width,  int height, char FillChar )
{
	// using nested loops, write this function
}

***end of code***

Recommended Answers

All 5 Replies

By the way, please use code tags around your code instead of typing "Code" and "end of code".

Anyways, I don't see what your problem is exactly. Don't you just need to actually code in whatever you want performed into the final function and you're all set?

The problem is creating the nested for loops really don't know what to do there..

I am trying to create a program that uses nested loops to create a box. Only one of the overloaded functions need the details of nested loops.. the other 3 should call the one that has the details.. my problem is the last function and the for nested loops.

Here is what I have so far

#include <iostream>
using namespace::std;

// Function prototypes

void displayBox ( int length );
void displayBox ( int length, char fillChar );
void displayBox ( int width, int height );
void displayBox ( int width, int height, char FillChar );

//--------------------------------------------------------------------

int main()
{
int boxLength, // Input box dimensions
boxWidth,
boxHeight;

char boxFill; // Input fill character

// Test the displayBox(length) and displayBox(length,fillChar)
// functions.
cout << endl << "Enter the length of a side: ";
cin >> boxLength;
displayBox(boxLength);
cout << endl << "Enter the fill character: ";
cin >> boxFill;
displayBox(boxLength,boxFill);

// Test the displayBox(width,height) and
// displayBox(width,height,fillChar) functions.
cout << endl << "Enter the width and height of the box: ";
cin >> boxWidth >> boxHeight;
displayBox(boxWidth,boxHeight);
cout << endl << "Enter the fill character: ";
cin >> boxFill;
displayBox(boxWidth,boxHeight,boxFill);

return 0;
}

//--------------------------------------------------------------------
void displayBox ( int length )
{
// call the 2 agrument function, int and char
char fill = " ";
displayBox(length, fill);
}


void displayBox ( int length, char fillChar )
{
// call the 3 argument function3
displayBox(length, length, fillChar);
}


void displayBox ( int width, int height )
{
// call the 3 argument function
char fil = " ";
displayBox(width, height, fill);
}

void displayBox ( int width, int height, char FillChar )
{
// using nested loops, write this function
}

This still makes no sense. You can't "call" the other functions to get the variables they had. And besides, you already passed them on to that last function anyways. And what exactly do you mean by "to create a box". Can you give what sample output would look like?

_ _ _ _ _
| |
| |
| |
_ _ _ _ _

_ _ _ _ _
| ? ? ? |
| ? ? ? |
| ? ? ? |
_ _ _ _ _


Inputs would be 5 ? 15 4 $

I'll formulate this below code based on the presumption that you're printing this and not storing it in a 2D array.

void displayBox ( int width, int height, char FillChar )
{
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            // Top or bottom row.
            if(y == 0 || y == height - 1)
                cout << "_";

            // In between top and bottom
            else {
                // Side
                if(x == 0 || x == width - 1)
                    cout << "|";
                // Filler
                else
                    cout << FillChar;
            }
        }
        cout << endl;
    }
}

Cycle through the the y-direction (the height, therefore each row) and then loop across the x-direction of that row (the width) and decide what to output. If it is the first or last row it will display the underscore "_". If it isn't, but if it is the side, it will display the pipe "|", and if it isn't that either, then it must output the FillChar. At the end of each row's x-direction, output a newline.

This worked perfectly for me when I tested it even though I've barely touched C++ :P (Python is for me). I hope that explained it well enough though - tell me if you need me to clarify it more!

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.