You should evaluate the number (removing the 1st digit) and then display it.
A simpler way may be converting the number into a string and then replace the leading characters with a space
typedefenum _TextAlignmentEnum
{
LeftAlignment,
RightAlignment,
CenterAlignment
} TextAlignmentEnum;
//I'm a Windows guy. I never write char *. I write LPSTR. LPCSTR means const char *.
LPSTR AlignText(LPCSTR szText, int columnSize, TextAlignmentEnum alignment)
{
LPSTR szResult;
if (strlen(szText) >= columnSize)
{
//Nothing to do. Return a copy of the original string.
szResult = newchar[strlen(szText) + 1];
strcpy(szResult, szText); //Or maybe the arguments are backwards. Double check this.
return szResult;
}
int whiteSpaceCount = columnSize - strlen(szText);
int leftWSCount;
switch (alignment)
{
case LeftAlignment:
//I leave this one for you to complete as exercise.
break;
case RightAlignment:
//Your question. I'll answer.
leftWSCount = whiteSpaceCount;
break;
case CenterAlignment:
//Also an exercise for you.
break;
}
szResult = newchar[columnSize + 1];
//I don't remember if there is an easier way to initialize the string.
for(int i = 0; i < columnSize; i++)
{
szResult[i] = ' ';
}
strcpy(&szResult[leftWSCount], szText);
return szResult;
}
The above function should return the specified text in the szText parameter, but padded to the left or right (or both) to achive the desired result within the specified imaginary (or not-so-imaginary) column size.
Dunno about that Bazzy. In real life, I only program C++ components, not entire applications, and much less console applications. My knowledge of C++ is 100% used to create Windows DLL's, either standard or ActiveX. :-)
Feel free to enlight me (and all of us) with an optimized version. The only consoles I ever did were the examples in "Programming Windows with C++" by Charles Petzold, and the guy uses printf().
With manipulators is really easy: cout << left << setfill('.') << setw(16) << "foobar";
In this line you give the alignment, the character which will fill the empty spaces, the length you want in a single line. After that, it will work with any type, not only for C strings
these manipulators would have done the job, but imho author forgot to tell us that this is his home assignment no. 3 (loops) - but that's only my guess :-)
something like here: http://www.cplusplus.com/forum/beginner/12597/