Thread: How to print Right aligned Text in a console?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy How to print Right aligned Text in a console?

    Hi, all

    How do you print a text to the right side of a console line? In other words how do you print right aligned text in a console? I do not want to use Windows console function because this process should also work in ms-dos.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    right is the manipulator for right justification. You may also need setw(). Have a play around. I dont use iostreams much.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    but how does this work when the screen size is changed, wich can be done in XP/2000?

    And when I try to compile this with VC++ it says:
    : error C2065: 'setw' : undeclared identifier

    What besides iostream.h do I need to include?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    setw is in iomanip

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You need iomanip.h
    Also, just assume the screen is 80x25. Otherwise you will have to call a bunch of Win32 console API's.

    Search this message board and look at the FAQ too.

    gg

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    ok thanks
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I think I meant 80x50...whatever a fullscreen console is.

    gg

  8. #8
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Fullscreen console is...

    ...80x25

    but printing anything on the 25th line will scroll the screen - it appears.
    Excuse me, while I water my money tree.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
       cout << "This is right alligned: *" << setiosflags(ios::right) << setw(10) << 123 << "*" << endl;
       return 0;
    }

  10. #10
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    how about

    Code:
    cout.setf(ios::right);
    nextus, the samurai warrior

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    whats wrong with just right?

    cout<<setw(40)<<right<<"This text is right justified"<<endl;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Angry What's wrong with it? THIS is what's wrong with it...

    IT DOESN'T WORK!

    Ok, forgive me. I'm a little flustered. I've been battling with this unconventional Borland C++ for a few days now and it's really getting on my nerves.

    The left or right parameters don't seem to work for me in Borland C++ 5.02. What nextus had suggested worked BEAUTIFULLY.

    Clearly there are different standards of C++ out there and I just happen to have the one that is the odd one out!! Just my luck. Do you know that I found myself going BACK to C's gets function to capture a line of text (with spaces) from the input stream because getline is BUG ridden?

    To me, the only thing C++ has over C is:

    1) Classes
    2) Inheritance
    3) Overloading Operators
    4) Polymorphism
    5) The String class/data type (finally! ..... char * is a pain in the...)

    ...and that's just about it. Aside from that, give me C anyday. If Borland's implementation of C++ is making me unjustly hate C++ then that just goes to show you it is like a standard that has yet to settle down.

    People tell me about the fix for certain modules and that they are in certain header files. When I check those header files, I realise the lines of code they refer to, don't even exist. I further realise that those lines of code, or even the definition for certain classes don't even exist in the header files they specify. They exist somewhere else!

    I ask a buddy for help, only to realise that the C++ he uses is wired for Hand Held application programming!

    Just how many standards of C++ are there out there with their third party modifications? I miss the days in the C forum when Prelude could tell me type this line of code and GOD knows it WILL work.

    Why? Because C has settled down and most everyone is using the same files, the same code, the same compiler/engine.

    With C++, people say type this line of code and when you try it you get some far out message that says:

    LEFT is NOT a member of the STD class
    I don't know about you guys, but I've had it with trying to get C++ to work. I'm going back to the tried and true functions in C that I know will work, like Gets instead of Getline and Printf instead of cout etc. etc. ..........

    I'll play with the classes and inheritance and polymorphism when I need to. But as far as I'm concerned, both languages are one in the same.

    Peace.
    Excuse me, while I water my money tree.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    There are many ways to do what you asked. All three ways suggested work. There is also only ONE standard. Third parties may add to it if they want, but its unlikely they'll substract.

    **** I suggest getting a newer compiler. ****

    left is not part of the std class, it's part of the std namespace (and the ios namespace).

    Classes is a reason why C++ is indefinatly better than C. C++ is a superset of C.

    And how about the vector, map, queue, stack, + classes? C++'s standard has so much more than what you studied so far.

  14. #14
    Registered User johnnyd's Avatar
    Join Date
    Mar 2002
    Posts
    70

    Smile You do have a point

    I cannot deny the usefulness of C++. Indeed I have studied a relatively limited portion of the superset that is C++ - a fundamental portion in any case.

    I have already taken the suggestion of many and am currently in the process of changing my compiler. I'm moving to Borland C Builer 6 in hopes that much of my headaches will disappear.

    Failing which, I may just whip out my Visual Studio Suite....

    ....again.
    Excuse me, while I water my money tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Scrolling text in a DOS CONSOLE box
    By Blizzarddog in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2004, 02:27 PM
  3. Underlining text in a Console Program.
    By Perica in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 09:46 AM
  4. Text based frame in win32 console
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 04-16-2002, 07:01 AM
  5. Console Text Scrolling Help!!
    By frgmstr in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2002, 02:37 PM