setw() and named constants

This is a discussion on setw() and named constants within the C++ Programming forums, part of the General Programming Boards category; How do you get setw() to work with named constants? I tried the following, but it isn't working. #include <iostream> ...

  1. #1
    Unregistered
    Guest

    Question setw() and named constants

    How do you get setw() to work with named constants?

    I tried the following, but it isn't working.

    #include <iostream> //Header file that contains cout and endl
    #include <string> //Header file that contains string commands
    #include <iomanip> //Header file that contains setw() function


    const string FIRST = "John"; //Person's First Name
    const string LAST = "Doe"; //Person's Last Name

    int main()
    {
    cout << setw(4) << FIRST << setw(4) << LAST <<endl;

    return 0;
    }

    Desired output:

    John Doe
    ----
    Thanks!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try this...
    cout << setw(5) << FIRST << setw(4) << LAST <<endl;

    or this
    cout << setw(4) << FIRST << right << setw(4) << LAST <<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

  3. #3
    Unregistered
    Guest

    Unhappy

    It still doesn't work

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    add this just below your includes....

    using namespace std;
    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

  5. #5
    Unregistered
    Guest

    Unhappy

    Didn't help.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    const string FIRST = "John";
    const string LAST = "Doe";
    int main()
    { 
    	cout << setw(4) << FIRST << setw(4) << LAST <<endl; 
    return 0; 
    }
    this works fine and produces expected output on msvc6 and msvc.net
    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

  7. #7
    Unregistered
    Guest

    Smile

    Thanks for all your help!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21