Center Function

So I saw some source code that allowed me to center any text I passed to a function and tried to recreate my own center function. However, I couldn't seem to do it so I found the code online and am quite confused as to why it's written the way it is. However, when I try and modify it, it doesn't print in the center. Here's the original working function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int center(const char *message)
{
    int width,i;

	width = strlen(message);
	if(width > 80)
        return - 1;

	cout.width((80 - width) / 2);

	// loop to print all characters in array
	for(i =0; i < width; i++, message++)
        cout << *message;
    cout << endl;
    return 0;

}
It works fine but I can't understand why removing the loop and replacing the "*message" with "message" prints the text to the left of the center
Last edited on
const char* message is the same as const char message[].

If you change it to const char message you have changed it from an array of characters to a single character. (And you are then very lucky that the program doesn't crash or do other untoward things to you.)

That function assumes your display is 80 characters wide. To find the number of spaces to place in front of the message, you must first find the number of spaces on either side of the message (screen width - message width) and then divide that by two.

Hope this helps.

Woops sorry I wasn't very clear. The *message I was referring to was immediately after the for loop. What I meant was, why put the loop when you could print all the characters at once using cout << message;? And why is it not centered when I write it this way? Sorry for the confusion.
Ah, it's because someone converted it from C, and didn't realize that he could just write cout << message;.

The centering trick problem you are having is that cout is trying to right-align whatever you print in a specific width. When printing character by character, the first character is printed properly and the remaining characters follow. When printing the whole message you get fail.

You can fix it by using the string constructor to print out the proper number of spaces first, or you can setw() to the value (80 - (80 - width) / 2).

Sorry.
That's no problem. This was pulled from a blackjack program written in C++. I don't know why whoever wrote it used C functions. So if I understand correctly, writing cout << message; prints the entire string at once rather than character by character?
From your point of view, yes.

The difference is in the width formatting. Consider printing the following things with a width of 10:

1
2
cout << setw(10) << 'h';
1234567890
         h
1
2
cout << setw(10) << "hello";
1234567890
     hello

In both cases, the text cursor is now positioned right after what was printed, so you can still cout more stuff and it will print exactly following.

1
2
cout << setw(10) << 'h';
cout << "ello world!";
1234567890
         hello world!
1
2
cout << setw(10) << "hello";
cout << " again!";
1234567890
     hello again!

Hope this helps.

[edit]
Oh, about the C-like stuff in C++: it is a common issue. C is a smaller and more well-known language, so people who are familiar with the way of doing things in C will often do them similarly in C++, even when a better way exists to do it in C++.
Last edited on
Topic archived. No new replies allowed.