LITTLEBLACKDOG.COM Forum Index LITTLEBLACKDOG.COM

 
LWD LWD   FAQ FAQ   Memberlist Memberlist   Usergroups Usergroups   Active Topics Active Topics   Register Register  
  Profile Profile   Log in to check your private messages Log in to check your private messages   Log in Log in  
  Who is Online Who is Online   Image Gallery Image Gallery   Chat Chat   Search Search  
  LWDGear       LBDGear  

View next topic
View previous topic
Post new topic     Reply to topic   LITTLEBLACKDOG.COM Forum Index » Code Warriors
Author Message
hosehead
Toilet Drinker
Toilet Drinker


Joined: 13 Jul 2002
Posts: 308

Post Posted: Sun Aug 03, 2003 5:00 am   Post subject: right-justifying text in C++ Reply with quote Back to top  

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.
View user's profile Send private message
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Sun Aug 03, 2003 4:12 pm   Post subject: Reply with quote Back to top  

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.
View user's profile Send private message Send e-mail
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Sun Aug 03, 2003 4:21 pm   Post subject: Reply with quote Back to top  

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.

Code: Select all
#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.
View user's profile Send private message Send e-mail
csign
Moderator
Moderator


Joined: 26 May 2001
Posts: 8155
Location: Borneo

Post Posted: Sun Aug 03, 2003 4:25 pm   Post subject: Reply with quote Back to top  

That is standard on linux too. But can you assume that to hold always true?
View user's profile Send private message Send e-mail
CMTG
Leg Humper
Leg Humper


Joined: 23 Feb 2002
Posts: 5438
Location: /var/log/cabin

Post Posted: Sun Aug 03, 2003 4:57 pm   Post subject: Reply with quote Back to top  

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
View user's profile Send private message Send e-mail Visit poster's website
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 27
Posts: 12389
Location: The Constitution State

Post Posted: Sun Aug 03, 2003 5:14 pm   Post subject: Reply with quote Back to top  

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.

Code: Select all
#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.

Code: Select all

#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;
}
View user's profile Send private message
hosehead
Toilet Drinker
Toilet Drinker


Joined: 13 Jul 2002
Posts: 308

Post Posted: Mon Aug 04, 2003 4:28 am   Post subject: Reply with quote Back to top  

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.
View user's profile Send private message
CMTG
Leg Humper
Leg Humper


Joined: 23 Feb 2002
Posts: 5438
Location: /var/log/cabin

Post Posted: Mon Aug 04, 2003 8:39 am   Post subject: Reply with quote Back to top  

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
View user's profile Send private message Send e-mail Visit poster's website
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 27
Posts: 12389
Location: The Constitution State

Post Posted: Mon Aug 04, 2003 10:03 am   Post subject: Reply with quote Back to top  

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 ;
View user's profile Send private message
Lycander
Lead Dog
Lead Dog


Joined: 24 May 2002
Age: 27
Posts: 12389
Location: The Constitution State

Post Posted: Mon Aug 04, 2003 10:04 am   Post subject: Reply with quote Back to top  

Another good read:

http://cs.stmarys.ca/~porter/csc/ref/cpp_io.html

I just did a google search for:

right justify console C++
View user's profile Send private message
brotherhobbes
Butt Sniffer
Butt Sniffer


Joined: 06 May 2002
Posts: 1579

Post Posted: Mon Aug 04, 2003 1:26 pm   Post subject: Reply with quote Back to top  

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.
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic     Reply to topic

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
phpBB SEO
All times are GMT - 8 Hours

Help us keep advertisements off this site. Donate today!