Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Do "setw(offset) " BEFORE cout ?

Do "setw(offset) " BEFORE cout ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    OK, I can do search and replace "cout<<" with "cout<< setw(offset)<< " to shift the output start column to console at offset. BUT anybody has an idea / hack how to PRESET "cout" ?

    int offset = 10;
    
    setw(offset);            // preset to offset , but this does not work , obviously 
    
    cout << setw(offset ) << "Test " <<  endl;
    cout << setw(offset ) << "Test " <<  endl;
    cout << setw(offset ) << "Test " <<  endl;
    
    offset += 10;
    cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
    cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
    cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
    cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
    
    L L CPalliniC 3 Replies Last reply
    0
    • V Vaclav_

      OK, I can do search and replace "cout<<" with "cout<< setw(offset)<< " to shift the output start column to console at offset. BUT anybody has an idea / hack how to PRESET "cout" ?

      int offset = 10;
      
      setw(offset);            // preset to offset , but this does not work , obviously 
      
      cout << setw(offset ) << "Test " <<  endl;
      cout << setw(offset ) << "Test " <<  endl;
      cout << setw(offset ) << "Test " <<  endl;
      
      offset += 10;
      cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
      cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
      cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
      cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No, The width feature affects the following item only. In the same way you cannot have a preset in printf.

      1 Reply Last reply
      0
      • V Vaclav_

        OK, I can do search and replace "cout<<" with "cout<< setw(offset)<< " to shift the output start column to console at offset. BUT anybody has an idea / hack how to PRESET "cout" ?

        int offset = 10;
        
        setw(offset);            // preset to offset , but this does not work , obviously 
        
        cout << setw(offset ) << "Test " <<  endl;
        cout << setw(offset ) << "Test " <<  endl;
        cout << setw(offset ) << "Test " <<  endl;
        
        offset += 10;
        cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
        cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
        cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
        cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
        
        L Offline
        L Offline
        leon de boer
        wrote on last edited by
        #3

        Usually when doing that sort of thing its easier to swing to ANSI which is semi portable on Windows and Linux consoles and you can have colours. Still cant set it global pre calls. An example

        printf("\x1B[%d;%df\x1B[31;40mRed line at 10,20\n", 10, 20);
        printf("\x1B[37;40m");

        In vino veritas

        V 1 Reply Last reply
        0
        • V Vaclav_

          OK, I can do search and replace "cout<<" with "cout<< setw(offset)<< " to shift the output start column to console at offset. BUT anybody has an idea / hack how to PRESET "cout" ?

          int offset = 10;
          
          setw(offset);            // preset to offset , but this does not work , obviously 
          
          cout << setw(offset ) << "Test " <<  endl;
          cout << setw(offset ) << "Test " <<  endl;
          cout << setw(offset ) << "Test " <<  endl;
          
          offset += 10;
          cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
          cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
          cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
          cout << setw(offset ) << "Test " <<"  space " << "Test " <<  endl;
          
          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Unfortunately, neither the setw manipulator nor the width[^] method is persistent. However, you may find workarounds here: c++ - "Permanent" std::setw - Stack Overflow[^].

          In testa che avete, signor di Ceprano?

          V 1 Reply Last reply
          0
          • CPalliniC CPallini

            Unfortunately, neither the setw manipulator nor the width[^] method is persistent. However, you may find workarounds here: c++ - "Permanent" std::setw - Stack Overflow[^].

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            That is little out of my league, but when I have a break I'll give it a try.

            1 Reply Last reply
            0
            • L leon de boer

              Usually when doing that sort of thing its easier to swing to ANSI which is semi portable on Windows and Linux consoles and you can have colours. Still cant set it global pre calls. An example

              printf("\x1B[%d;%df\x1B[31;40mRed line at 10,20\n", 10, 20);
              printf("\x1B[37;40m");

              In vino veritas

              V Offline
              V Offline
              Vaclav_
              wrote on last edited by
              #6

              Leon, I am getting used to using cout, despite of its irrational behaviour. In my main project I went overboard with debugging colouring scheme and it sort of works. I just started the "client" part of the project and reusing a class from my main project where cout colouring and cout lines work just as expected. In derived project - no coloring, cout lines have extra line feed and I am getting non printable characters to boot. Just another bug to find. BTW I am finding out this setw does not always work....

              L 1 Reply Last reply
              0
              • V Vaclav_

                Leon, I am getting used to using cout, despite of its irrational behaviour. In my main project I went overboard with debugging colouring scheme and it sort of works. I just started the "client" part of the project and reusing a class from my main project where cout colouring and cout lines work just as expected. In derived project - no coloring, cout lines have extra line feed and I am getting non printable characters to boot. Just another bug to find. BTW I am finding out this setw does not always work....

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Vaclav_ wrote:

                I am finding out this setw does not always work

                More likely, you are finding there are bugs in your code.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups