Hey guys.

I am making a simple console canender program for an exercise from a book, and it is going ok so far but I am having trouble with the spacing of it.

I have made a simple function that creates on day block like a normal calender would have, and then in main called this 30 times to print the calender out. I tried using a nested for loop as I would with arrays to create the boxes acorss the screen but they seem to only appear down the side of the screen when I run it.

It is quite a simple problem I am sure of it, can you see what I am doing wrong?

#include <iostream>

// function prototypes
void printBlock ( int );

// main function - driver /////////////////////////////////////////////////////
//
int main ( void ) {
	for ( int i = 0; i < 1; i++ ) {
		for ( int j = 1; j <= 30; j++ ) {
			printBlock ( j );

			if ( j % 7 == 0 ) {
				std::cout << "\n";
			}
		}
		std::cout << std::endl;
	}

	std::cin.get();

	return 0;
}

// function to print a single block of the calender
void printBlock ( int x ) {
	for ( int i = 1; i <= 7; i++ ) {
		std::cout << "*";
	}

	std::cout << "\n";

	for ( int i = 1; i <= 7 -2; i++ ) {
		std::cout << "*";
		for ( int j = 1; j <= 7 -2; j++ ) {
			std::cout << " ";
		}
		std::cout << "*\n";
	}

	for ( int i = 1; i <= 7; i++ ) {
		std::cout << "*";
	}
}

Dani AI

Generated

The behavior comes from printing an entire block (all its newlines) for each day before moving to the next. That naturally stacks blocks vertically. As hinted, draw the calendar by printing horizontal slices across all columns in the same week: print the top borders of every day in the week, newline, then the first interior line for every day, newline, and so on. That way each block's corresponding lines appear on the same console row.

Below is a concise C++ example that implements this idea. It prints 30 day boxes, 7 per week, and centers the day number inside the box. Adjust BOX_W / BOX_H to change appearance.

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    const int TOTAL = 30;
    const int PER_WEEK = 7;
    const int BOX_W = 7;
    const int BOX_H = 7;

    for (int start = 1; start <= TOTAL; start += PER_WEEK) {
        int count = std::min(PER_WEEK, TOTAL - start + 1);

        for (int line = 0; line < BOX_H; ++line) {
            for (int d = 0; d < count; ++d) {
                int day = start + d;
                if (line == 0 || line == BOX_H - 1) {
                    std::cout << std::string(BOX_W, '*');
                } else if (line == 2) { // put day number on a chosen interior line
                    std::string inner(BOX_W - 2, ' ');
                    std::string s = std::to_string(day);
                    int pos = (int)((inner.size() - s.size()) / 2);
                    inner.replace(pos, s.size(), s);
                    std::cout << '*' << inner << '*';
                } else {
                    std::cout << '*' << std::string(BOX_W - 2, ' ') << '*';
                }
                std::cout << ' '; // gap between boxes
            }
            std::cout << '\n';
        }
        std::cout << '\n';
    }
}

Troubleshooting tips: build lines as strings first if debugging; use std::string(n, ch) to repeat characters; use std::setw or manual center logic for alignment. If boxes still print vertically, inspect where newlines occur—if a function emits multiple \n for a single column, switch to the slice-by-slice approach shown.

What do you mean? Do you want the blocks to appear in lines, like that?:

******  ******  ******  ******  ******
*    *  *    *  *    *  *    *  *    * 
*    *  *    *  *    *  *    *  *    * 
******  ******  ******  ******  ******

If you do, you need to print multiple blocks drawing-lines before finished the line.

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.