CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Simple iostream format question

    Hello,

    How can output to iostreams (example std::cout) be wrapped around and appear on the same line?

    In particular, I would like to print some ticker sequence like

    Number: 0.0
    Number: 0.1
    Number: 0.2

    all on one line such that the "Number: " part remains steady and the decimal ticker part counts up. I guess the ideal solution would only print the "Number: " part once and cleverly back up over the decimal ticker part. I tried some stuff with '\r', but didn't like the lack of elegance of the implementation. In addition, with '\r' if the results of previous outputs were longer than the present outputs, then the characters will not be erased and they remain as ghost images.

    The sample code prints a separate line for each ticker. Can anyone modify the sample code accordingly for the desired ticker effect. I would really appreciate any help.

    Thanks a lot.
    Chris.



    Code:
    #include <iostream>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
      std::cout << endl;
    
      for(volatile int n = 0; n <= 100; n++)
      {
        std::cout << "Number: "
                  << double(n) / 10
                  << endl;
      }
      
      return n;
    }
    You're gonna go blind staring into that box all day.

  2. #2
    Join Date
    Aug 2002
    Location
    VA, USA
    Posts
    137
    dude_1967,

    Kinda swamped today so I can't modify your code. But have
    you tried using the backspace charactor (\b)? I used it a
    long time ago to display a console like program progress
    baton using printf.

    regards, willchop

  3. #3
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Lightbulb use this

    well i think this'll help ya
    note: this was originally posted to me by zdf
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
      double d = 123.45;
      float f = 67.89f;
      cout << d << endl // 123.45
           << f << endl; // 67.89
      cout << fixed << setprecision(1) 
           << d << endl // 123.4
           << f << endl; // 67.8
      return 0;
    }
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Thumbs up Solved

    Thanks.

    Used a combination of '\b' and ::std::setprecision() to accomplish the format.

    Chris.

    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured