Thread: setw newb help

  1. #1
    Registered User
    Join Date
    Oct 2003

    Question setw newb help

    Sorry about this, I'm very new to the c++ world. In all the books I've read and even the post I've found here, setw looks easy - but it's not working for me.

    cout <<setw(20) <<"Fanzone Amusement Ticket Entry\n";
    I'm trying to make "Fanzone Amusement Ticket Entry" sit in the, approximate, middle of a console app. I've been successfull by using spaces after the " and before the F but I know there's gotta be a better way.

    When I use setw like above, I don't recieve any errors but "Fanzone Amusement Ticket Entry" stays to the far left.

    Does setw not work with strings? If not, what will?

    Any assistance is appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    You could always do something like this:
    #include <string>
    #include <iomanip>
    #include <iostream>
    using namespace std;
    int main()
        string title("Fanzone Amusement Ticket Entry\n");
        cout << setw(40+title.length()/2) << title;
        return 0;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    it;s because setw moves the print cursor over by the amount of spaces (20) and starts printing... the words move to the left... set the number higher and it should work fine...
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Oct 2003

    Thank you both..

    Thank you both. Never thought of the first way and didn't know the second would work. Both work like a champ though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb question
    By C_ntua in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2008, 09:44 AM
  2. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  3. Newb C++ Programmer
    By Philandrew in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 08:44 PM
  4. Problem with setw
    By PJYelton in forum C++ Programming
    Replies: 3
    Last Post: 10-15-2002, 02:56 PM
  5. setw() and named constants
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 02-11-2002, 01:22 PM
Website Security Test