Thread: Tabs in program

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    12

    Tabs in program

    I was wondering if it was possible to have "tabs" inserted to line up text in your program. I would appreciate any help that someone can give me. Thank you.

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Re: Tabs in program

    Originally posted by CompWiz84
    I was wondering if it was possible to have "tabs" inserted to line up text in your program. I would appreciate any help that someone can give me. Thank you.
    I believe you are talking about text you output with cout, right?

    If so, you can use 2 techniques. I rather much prefer the second one.

    1. Use the '\t' (tab) character exactly the same way as you use the '\n' (new line).

    cout << "My mon is" << "\t123 years old";

    2. Use the setw() manipulator

    cout << "My mom is" << setw(15) << "123 years old";

    This second option will place the string constant "123 years old" inside a 15 character wide field. It aligns, by default, to the left. So, in order for it to align to the right, you do:

    cout << "My mom is" << right << setw(15) << "123 years old"

    'right' is also a I/O manipulator. Study them. They are quite useful when using cout and cin.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    Thanks, Mario.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    I tried both ways. The first one, /t, worked fine. I could not get the setw() way to work, it said that "right" and "setw" were undeclared identifiers. If you know what I did wrong, i would appreciate the furthur help. For now I will use /t. Source code below:
    ==============================================
    #include<iostream.h>

    int main()
    {
    cout << "Column A" <<right<< setw(15) << "Column B";
    return 0;
    }
    ==============================================

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    isnt setw() in iomanip.h?
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You have to include <iomanip>

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    I included the iomanip.h file, but it still won't let me align with "right", saying that it is an undeclared variable here is my code, i am sure it is something simple, but i am pretty new at C++. Thanks for any help. Sorce code below:
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    int main()
    {
    	cout << "Column A" << right<< setw(15) << "Column B";
    
    	return 0;
    }

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Take away the .h in iomap and see what happens

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    12
    Changing the iomanip.h to iomanip did not do anything but make "setw" an undefined variable, along with "right". I have tried to place the right in other places, and I also opened the iomanip.h file to see if I could figure it out from the code syntax there, but the word "right" was not there. My source code is the same as above so I will not repost it. Perhaps I cannot align it after all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM