简体   繁体   中英

How would i print this on the same line

I am trying to write a program that builds a "building" using multiple characters. There are 3 functions:

  1. int ValidateData(int low, int high, char type)

This function validates that the user's entry meets the required parameters and returns the correct number depending on the type parameter.

No problems here.

  1. int drawUpperFloors(int numOfWindows)

This function is supposed to draw the number of floors depending on how many windows the user entered. My problem is that I cannot seem to get the windows to draw on the same line. If the user entered 3 the output should be: (Took awhile to get that right)

+---------------+ | | | === === === | | | | | | | | | | === === === | | |

Instead I get a window on a new line until the loop reaches its limit ( numOfWindows ), so if numOfWindows = 20 then I get 20 windows each on a different line. I would appreciate insight into how I might do this.

  1. void DrawGroundFloor(int numOfWindows)

    This function is almost identical to the drawUpperFloors( ) function except that the first window will be replaced with a door. So, if the numOfWindows parameter is 2, then the ground floor will consist of 1 door and 1 window. If the number of windows is only 1, then only a door will be drawn.

    Please take note of the following details: a). Each window (and door) is exactly 3 columns wide.

    b). The bottom of the floor is drawn using the * character.

    An example of a ground floor with 5 windows woulb be:

+-----------------------+ | | | === === === === === | | | | | | | | | | | | | | | | === === === === | | | | | *************************

Considering I have been unable to do the second function this comes as no surprise.

I have tried using up to 3 loops using if and else , but I still cannot, no matter what I try (even modulus) get it print in the same line. Any input would be appreciated.

EDIT here's the code for the 2 functions so far

int GetValidData(int low, int high, char type)
{
    int number;
    int number2;

    int i=1;
    int j;
    if (type == 'F')
    {
        printf("Enter the amount of floors you would like(1-1000)\n");
        scanf("%d", &number);
        fflush(stdin);


        if (number < 1 || number>1000)
        {
            while(i!=0)
            {
                printf("You have entered an invalid floor number\nPlease enter a valid number: ");

                scanf("%d", &number);
                fflush(stdin);
                if (number >= 1 && number <= 1000)
                {
                    printf("Thank you!\n");
                    i = 0;

                }
            }

        }
        return number;
    }



    printf("Enter the amount of windows you would like(1-20)\n");
    scanf("%d", &number);


    if (type == 'W')
    {
        if (number < 1 || number>20)
        {
            while(i!=0)
            {
                printf("You have entered an invalid number\nPlease enter a valid number: ");
                scanf("%d", &number);
                if (number >= 1 && number <= 20)
                {
                    printf("Thank You!\n");
                    i = 0;
                }
            }

        }

        return number;




int drawUpperFloors(int numOfWindows)
{
    int check = numOfWindows;
    int i;
    int j;
    for (i = 0; i <= numOfWindows;i++)
    {
        printf("===\n");
        for (j = 0; j <= numOfWindows;j++)
        {
            printf("|  |\n");
        }

        printf("===");
    }

}

My problem is that I cannot seem to get the windows to draw on the same line.

The basic problem is you're adding newlines to your print statements when drawing an individual window.

for (i = 0; i <= numOfWindows;i++)
{
    /* There shouldn't be a newline here */
    printf("===\n");
    for (j = 0; j <= numOfWindows;j++)
    {
        /* Nor a newline here */
        printf("|  |\n");
    }

    printf("===");
}

You're also counting your windows from 0 which results in one too many windows. Either start from 1 or go until i < numOfWindows .

You have to draw each line in its own loop, including the outer walls, ending with a newline. First the upper sills, then the windows, then the lower sills.

void drawUpperFloors(int numOfWindows)
{
    int i;

    /* Draw the upper sills, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("=== ");
    }
    printf(" |\n");

    /* Draw the windows, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("| | ");
    }
    printf(" |\n");

    /* Draw the lower sills, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("=== ");
    }
    printf(" |\n");
}

That gets repetitive, so it's best to put drawing a line of a floor into a function.

void drawFloorLine(int num, const char *to_repeat) {
    int i;

    printf("|  ");
    for (i = 1; i <= num;i++)
    {
        printf("%s", to_repeat);
    }
    printf(" |\n");
}

void drawUpperFloors(int numOfWindows)
{
    drawFloorLine(numOfWindows, "=== ");    
    drawFloorLine(numOfWindows, "| | ");
    drawFloorLine(numOfWindows, "=== ");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM