| Author |
Message |
hosehead
Toilet Drinker


Joined: 13 Jul 2002 Posts: 308
|
Posted:
Sun Aug 03, 2003 5:00 am Post subject: right-justifying text in C++ |
|
How would I (Micaela, actually) right-justify screen output in C++?
It's been a couple years and I'm really rusty. Would I need to use the printf command?
For the record, I'm not a programmer...just trying to get my girlfriend through C++. She isn't a programmer either. She very well may hate C++ more than I did. Having said that, I hate it less now that I'm helping her instead of doing it because I have to.
But I digress...back to the question:
How would I right-justify screen output in C++?
Thanks for any suggestions. |
|
|
|
|
|
|
brotherhobbes
Butt Sniffer

Joined: 06 May 2002 Posts: 1579
|
Posted:
Sun Aug 03, 2003 4:12 pm Post subject: |
|
well there are 80 characters in the dos box (i take it you are using windows and haven't changed the screen buffer size), so you can just count the number of characters that are being displayed, subtract that from 80, and add that many spaces. |
|
|
|
|
|
|
brotherhobbes
Butt Sniffer

Joined: 06 May 2002 Posts: 1579
|
Posted:
Sun Aug 03, 2003 4:21 pm Post subject: |
|
here, i'm bored, so i wrote up a quick version of what i'm talking about. maybe there's a function that does this for you, but i don't know it off the top of my head.
#include <iostream>
using namespace std;
int main(void)
{
char text[80] = { 0, };
// get text
cout << "Please enter text to right justify: ";
cin >> text;
// count num chars in text, add spaces as neccessary
int iSpaces = 80 - strlen(text);
for (int i = 0; i < iSpaces; i++)
cout << ' ';
// now text is "right justified"
cout << text;
return 0;
}
if this isn't what you are talking about, lemme know. |
|
|
|
|
|
|
csign
Moderator


Joined: 26 May 2001 Posts: 8155
Location: Borneo
|
Posted:
Sun Aug 03, 2003 4:25 pm Post subject: |
|
That is standard on linux too. But can you assume that to hold always true? |
|
|
|
|
|
|
CMTG
Leg Humper


Joined: 23 Feb 2002 Posts: 5438
Location: /var/log/cabin
|
Posted:
Sun Aug 03, 2003 4:57 pm Post subject: |
|
csign wrote:That is standard on linux too. But can you assume that to hold always true?
Usually true for windows console, but it's quite frequently not 25 rows down. |
_________________ Pie. I wish I could
constrain my hungry greed but...
Sadly, defeated.
Charlene's Law: There's no such thing as can't.
Charlene's Corollary: Unless it's followed by be arsed.
I write more quotes than a fucking big book of quotes. - Scroobius Pip
|
|
|
|
|
Lycander
Lead Dog


Joined: 24 May 2002 Age: 27 Posts: 12389
Location: The Constitution State
|
Posted:
Sun Aug 03, 2003 5:14 pm Post subject: |
|
brotherhobbes wrote:here, i'm bored, so i wrote up a quick version of what i'm talking about. maybe there's a function that does this for you, but i don't know it off the top of my head.
#include <iostream>
using namespace std;
int main(void)
{
char text[80] = { 0, };
// get text
cout << "Please enter text to right justify: ";
cin >> text;
// count num chars in text, add spaces as neccessary
int iSpaces = 80 - strlen(text);
for (int i = 0; i < iSpaces; i++)
cout << ' ';
// now text is "right justified"
cout << text;
return 0;
}
if this isn't what you are talking about, lemme know.
I'm an optimize freak, so here's my mod of the above code that eliminates the for loop.
#include <iostream.h>
#include <memory.h>
int main(void)
{
char text[80];
char buffer[80];
// make sure buffers are clean
memset(text, 0, sizeof(text));
memset(buffer, 0, sizeof(buffer));
// get text
cout << "Please enter text to right justify: ";
cin >> text;
int iSpaces = 80 - strlen(text); // calc # of blank spaces
memset(buffer, ' ', iSpaces); // fill the first iSpaces chars of buffer with a blank space
strcat(buffer, text); // concatenate the input text to the now justified string
cout << buffer;
}
|
|
|
|
|
|
|
hosehead
Toilet Drinker


Joined: 13 Jul 2002 Posts: 308
|
Posted:
Mon Aug 04, 2003 4:28 am Post subject: |
|
Wow...thanks for the help. But this will surely get the instructor's attention. I can't recall how I did this a few years back when I was taking C++, but it was probably one command rather than a separate function. The suggestions, while feasible, scream "Somebody helped me!!!"
What's the simplest way?
Thanks for the help folks...I've been working 90+ hours per week and don't have a lot of "extra" time. I know Micaela hates it. I'll probably slap my forehead and say "Doh!!!!" when I see the command that I'm thinking of. |
|
|
|
|
|
|
CMTG
Leg Humper


Joined: 23 Feb 2002 Posts: 5438
Location: /var/log/cabin
|
Posted:
Mon Aug 04, 2003 8:39 am Post subject: |
|
hosehead wrote:Wow...thanks for the help. But this will surely get the instructor's attention. I can't recall how I did this a few years back when I was taking C++, but it was probably one command rather than a separate function. The suggestions, while feasible, scream "Somebody helped me!!!"
What's the simplest way?
Thanks for the help folks...I've been working 90+ hours per week and don't have a lot of "extra" time. I know Micaela hates it. I'll probably slap my forehead and say "Doh!!!!" when I see the command that I'm thinking of.
I don't think C++ natively has a single command to do that because of it's platform independent-ness. Have a look in the PlatformSDK docs in MSDN if you are using windows. http://msdn.microsoft.com/library/default.asp |
_________________ Pie. I wish I could
constrain my hungry greed but...
Sadly, defeated.
Charlene's Law: There's no such thing as can't.
Charlene's Corollary: Unless it's followed by be arsed.
I write more quotes than a fucking big book of quotes. - Scroobius Pip
|
|
|
|
|
Lycander
Lead Dog


Joined: 24 May 2002 Age: 27 Posts: 12389
Location: The Constitution State
|
Posted:
Mon Aug 04, 2003 10:03 am Post subject: |
|
I think I found something:
http://cisnet.baruch.cuny.edu/friedman/cplusplus/n_filesnformatg.doc
I/O manipulators - use <iomanip.h> header file
setfill(ch) set the fill character to ch
setw(n) set the field width to n
setprecision(n) set the floating-point precision to n places
setiosflags(flags) set the format flags for the ios [input/output stream]
Note: The setiosflags manipulator is also called a parametized manipulator, since it has parameters (arguments).
format flags for setiosflags()
[these flags may be combined by | operators]
ios::showpoint always show the decimal point (default is 6 decimal digits)
ios::showpos display a leading + sign when the number is positive
ios::fixed display up to 3 integer digits and 2 digits after the decimal point.
(For larger values, use exponential notation.)
ios::scientific use exponential notation to display output
ios::showbase show base indicator on output
ios::left left justify output
ios::right right justify output
ios::skipws skip whitespace on input
--------------------------------------------------------------------
http://216.239.33.104/search?q=cache:NFi7LAK1oZIJ:www.cs.umbc.edu/courses/undergraduate/CMSC202/spring03/Discussions/Week2.ppt+right+justify+console+C%2B%2B&hl=en&ie=UTF-8
Formatted Output … cont
Justification
Uses stream manipulators left and right
left : left-justify, padding characters on right
right: right-justify, padding characters on left
cout << left << setw(10) << x ;
cout << right << setw(10) << x ; |
|
|
|
|
|
|
Lycander
Lead Dog


Joined: 24 May 2002 Age: 27 Posts: 12389
Location: The Constitution State
|
Posted:
Mon Aug 04, 2003 10:04 am Post subject: |
|
|
|
|
|
brotherhobbes
Butt Sniffer

Joined: 06 May 2002 Posts: 1579
|
Posted:
Mon Aug 04, 2003 1:26 pm Post subject: |
|
so this is for some class? then do it the simplest way possible. no reason to get a prof suspcious of her cheating.
anyways, if they've covered the ios flags part, that would be your best bet. before you output the text to be right justified, just do something like
cout.setf(ios::right);
cout << text;
or
cout << setiosflags(ios::right) << etc;
if not, use my shitty for loop or some other easy way of doing it, unless they have covered memset. then do it the super fly ly way. |
|
|
|
|
|
|
|
|