Alignment Help

I need help with making the output be aligned
Sorry im new

{
cout << "--------- OPTIONS AND ACCESSORIES ----------------\n\n";
cout << setw(20) << left << "\"PowerSlide\" Moon Roof" << setw(10) << left << "$ " << g_SunRoof << endl;
cout << setw(20) << left << "Voice-Activated Navigation System" << setw(10) << left << "$ " << g_NavSystem << endl;
}

I needs to look like this:
--------- OPTIONS AND ACCESSORIES ----------------
             "Powerslide" Moon Roof        $ 460
  Voice-Activated Navigation System        $ 245
Last edited on
Don't use "left"
Instead, use the tab: '\t'

1
2
3
4
5
6
#include <iostream>

int main() {
     std::cout << "Hello World!\t\tHow are you?" << endl;
     return 0;
}
rlc4 is partially right, but tab will not help. Try replacing the first left in each line with right. You are trying to align to the right after all, if I understand right.
Could you explain why tab would not help. I used to use it all the time when doing console "beautification code" with no issues.
You could also pretty easily program something to automatically put blank spaces in yourself! It could make a great learning experience and you would also know how to use it!
That's true. What was it called again? "String Buffering" or something? "Padding"?

It's where the program would set up a set of spaces (or any other character one would choose), and then it would replace some of them with the actual string.

An example of doing it with binary:
printf("%08d", 100101);

But, you know, with spaces.

P.S. I retract my statement on "Don't use 'Left'." I had a very "archaic" C++ book that used printf. After doing a small search to make sure I'm doing it right, I realized that using printf() was primarily a C thing, and C++ has what the OP was using.
So, there is the old C-Way of doing it :D

...behold, a post that really didn't contribute much to the progression of this thread!

EDIT
On second thought, I might not COMPLETELY retract it, because instead of Left, use Right.
Last edited on
Tab won't help because his teacher is being extremely specific in the positions he wants the text. If you use Tab you wont be able to get to that specific position you will get to the position that Tab has built in to increment to (whatever that may be). After screwing with some numbers for about 10 minutes I got the result you're looking for.

1
2
3
4
5
6
int p1 = 460;
int p2 = 245;

cout << "--------- OPTIONS AND ACCESSORIES ----------------\n\n";
cout << setw(35) <<  "\"PowerSlide\" Moon Roof" << setw(12) << right << "$ " << right << p1 << endl;
cout << setw(25) <<  "  Voice-Activated Navigation System" << setw(12) << right << "$ " << right  << p2 << endl;

Could you explain why tab would not help.


1
2
3
4
5
6
7
#include <iostream>

int main() {
     std::cout << "Hello World!\t\tHow are you?" << endl;
     std::cout << "Hello World, Hello!\t\tHow are you?" << endl;
     return 0;
}


That puts 2 tabs after each part of the strings, it does not align them. You would have to know the length of each bit and tailor it to each line. setw(), and right/left is what you need to use for formatting text in C++. The only problem is if you do not use a proper value for setw().
Last edited on
Thank you guys so much the problem was the numbers I was using in setw()
Topic archived. No new replies allowed.