Thread: Right aligning.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Right aligning.

    Hello I am trying to right align a program so that the input is 20spaces over on the console. So for an example:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    int main()
    
    {
    
    string name, address;
    
    cout << "Enter your name: " ;
    cin >> name;
    cout << "Enter your address: ";
    cin >> address;
    
    cout << "Your name and address is: " << setw(20) << name << endl;
    cout << setw(30) << address endl;
    
    }
    I want name and address to align up perfectly, but the problem is, unless the user's input of name and address are exactly the right amount of spaces, they won't align up properly.

    So how do you make it so no matter how many number of characters their name and address is, they align properly underneath each other?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You said it yourself. Discard the extra spaces if encountered.

    EDIT: I mean, inform the user that there's a limit for the characters he/she can type, and if he/she doesn't comply you erase the trailing ones.
    Last edited by GReaper; 02-12-2011 at 12:48 PM.
    Devoted my life to programming...

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    you'd have to remove the string Enter your address from the output line so it won't throw off the calculations.

    Code:
    #include<iostream>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    int main()
    
    {
    
    string name, address;
    int base = 40;
    cout << "Enter your name: " ;
    cin >> name;
    cout << "Enter your address: ";
    cin >> address;
    
    cout << "Your name and address is: " << endl << setw(base+name.length()) << name << endl;
    cout << setw(base+address.length()) << address << endl;
    return 0;
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 06-13-2010, 12:51 PM
  2. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 08:09 PM
  3. aligning a table?
    By Axel in forum C Programming
    Replies: 5
    Last Post: 10-19-2005, 03:30 AM
  4. Aligning strings display format
    By Hexxx in forum C Programming
    Replies: 2
    Last Post: 11-03-2003, 10:51 PM
  5. Aligning text
    By Bad_Scooter in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2003, 12:06 PM