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.
Printable View
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?Quote:
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.
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.
Thanks, Mario.
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;
}
==============================================
isnt setw() in iomanip.h?
You have to include <iomanip>
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;
}
Take away the .h in iomap and see what happens
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.