Thread: HELP with text formating

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10

    Unhappy HELP with text formating

    Hey all , I started to learn C++ recently and I wud need some help on something.

    Here is my file.txt:
    Code:
    Allemagne             357021     83251851         Euro                          
    Australie            7686850     19546792         Dollar_australien           
    Belgique               30510     10274595         Euro                                    
    Egypte               1001450     70712345         Livre_egyptienne
    ------------------------------------------------------------------------------------------------------
    Here is my code:
    Code:
    int main()
    {
       string filename,country,money;
       int population,sup;  
       cout << "Wuts the name of the file to read ?" << endl;
       cin >> filename;
       
       ifstream reading;
       reading.open(filename.c_str());
       if(reading.fail()) {
          cerr << "Error cudnt open the file" << filename
               << endl;
          exit(EXIT_FAILURE);   
       }
       
       while(reading >> country >> sup>> population>> money){
          cout << country<< " "<<sup<< " "<<  population << " "<<money<< " "<< endl;
       }
       
       reading.close();
    ------------------------------------------------------------------------------------------------------
    Here is my question:

    If I want the text after i read it to be in the same format as the original (the same alignment) how can I do? I know the command setw() but it only do for example ___country ______money ... Hope someone can answer
    Last edited by eezaa; 11-08-2007 at 06:25 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Edit your post and put code tags around the contents of file.txt so we can see the proper alignment (otherwise the forum software removes extra spacing).

    To fix the alignment, you'll probably need to use io manipulators from <iomanip>. Perhaps setw, or a combination of setw and setfill will help.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10
    ya i noticed my text wasnt aligned .. well this is my problem isnt it LOL ... fixed it hehe

    Well I know about setw but it align to right... how do I align to left???

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is another io manipulator that will adjust that, but I don't know which it is off the top of my head.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It might be std::left.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10

    Thumbs down

    its still not working .... any idea??? well at least I guess im not the only one to ignore the answer lol

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest effort, not just an information-free "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To left-align:
    Code:
    std::cout << std::left << std::setw(10) << object;
    I'm pretty sure that left and setw can be in any order, but they have to be sequential.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10
    lol my lastest effort ... ya ok i tried this command:
    Code:
    setf(ios::left)
    and it gave me : error primary expression expected before.... and i included iostream,iomanip and namespace std ... so i didnt get that part ... honnestly i never had to format any text before so I have no clue how to use those... all I used was setw() but it doesnt seem to be enough to solve my problem...
    Last edited by eezaa; 11-09-2007 at 09:48 PM.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm, it seems I was wrong (from http://www.fredosaurus.com/notes-cpp...ipulators.html):
    [std::]left: Left justifies output in field width. Only useful after setw(n).
    Try this, see what happens.
    Code:
    std::cout << std::setw(10) << std::left << object;
    Unfortunately, I don't have a compiler on this computer, but Dinkumware doesn't seem to mind this:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    int main() {
        const double pi = std::atan(1) * 4;
        std::cout << std::setw(10) << std::left << pi;
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM