Topic: COUT PROBLEM


Author: "Bao_Le" <bao_le@email.msn.com>
Date: 1997/09/23
Raw View
Hi Rich Bielawski,

I saw your posting on the newsgroup comp.std.c++. I've fixed the bugs so
that the code you wrote could work the way you want it to.

#include <iostream.h>
#include <iomanip.h>


while (N = NextNumber())
 cout  << setw(9) << setprecision(3)
  << setiosflags (ios::showpoint | ios::fixed)
  << (T += N) << endl;


Hope it'll help you out. Good luck!

Bao Le J
(www.aaawebs.com/baole/index.htm)


Rick Bielawski <rbielaws.leadbelt.com@home.com> wrote in article
<01bcc5c3$4f167e20$3436e6cf@PCI-150.leadbelt.com>...
> I hope somebody can set me straight.  I get no response from the
> groups that appear to be more appropriate.
>
> Is there a way to make the following do what I think it ought to?
>
> while (N = NextNumber())
>   cout << setw(9) << setprecision(3) << (T += N) << "\n";
>
> With the following numbers: 123.4, 1.6, 1.8
> OUTPUT LOOKS LIKE THIS!
>
>     123.400
>         125
>     126.800
>
> How can I prevent it from ignoring my setprecision(3) when the
> number being formatted happens to contain zeros to the right
> of the decimal point?
> --
> Rick Bielawski
> Tandem Automation Specialist
> ---
> [ comp.std.c++ is moderated.  To submit articles: try just posting with
   ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu
   ]
> [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
   ]
> [ Policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu
   ]
>
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Rick Bielawski" <rbielaws.leadbelt.com@home.com>
Date: 1997/09/21
Raw View
I hope somebody can set me straight.  I get no response from the
groups that appear to be more appropriate.

Is there a way to make the following do what I think it ought to?

while (N = NextNumber())
  cout << setw(9) << setprecision(3) << (T += N) << "\n";

With the following numbers: 123.4, 1.6, 1.8
OUTPUT LOOKS LIKE THIS!

    123.400
        125
    126.800

How can I prevent it from ignoring my setprecision(3) when the
number being formatted happens to contain zeros to the right
of the decimal point?
--
Rick Bielawski
Tandem Automation Specialist
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/09/23
Raw View
"Rick Bielawski" <rbielaws.leadbelt.com@home.com> writes:

|>  I hope somebody can set me straight.  I get no response from the
|>  groups that appear to be more appropriate.
|>
|>  Is there a way to make the following do what I think it ought to?
|>
|>  while (N = NextNumber())
|>    cout << setw(9) << setprecision(3) << (T += N) << "\n";
|>
|>  With the following numbers: 123.4, 1.6, 1.8
|>  OUTPUT LOOKS LIKE THIS!
|>
|>      123.400
|>          125
|>      126.800
|>
|>  How can I prevent it from ignoring my setprecision(3) when the
|>  number being formatted happens to contain zeros to the right
|>  of the decimal point?

Do "cout.setf( ios::fixed , ios::floatfield )" before the loop.

Basically, the default formatting for floats corresponds to the "%g"
format specifier in printf -- what you want is "%f".

Also, don't forget to restore the flags you modify.  Later users of the
stream in your code will thank you.  (I have a class GB_IOSave that I
use for this.  That way, the flags are restored even in case of an
exception.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
        I'm looking for a job -- Je recherche du travail
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/09/23
Raw View
Hi,
Rick Bielawski (rbielaws.leadbelt.com@home.com) wrote:
: I hope somebody can set me straight.  I get no response from the
: groups that appear to be more appropriate.

What groups did you try?. comp.lang.c++.moderated is probably a good
place but I haven't seen your article...

: Is there a way to make the following do what I think it ought to?

: while (N = NextNumber())
:   cout << setw(9) << setprecision(3) << (T += N) << "\n";

: With the following numbers: 123.4, 1.6, 1.8
: OUTPUT LOOKS LIKE THIS!

:     123.400
:         125
:     126.800

: How can I prevent it from ignoring my setprecision(3) when the
: number being formatted happens to contain zeros to the right
: of the decimal point?

I guess you want to set float formatting to fixed:

  cout.setf(ios_base::fixed, ios_base::floatfield);

This will print the numbers like:

     123.400
       1.600
       1.800

BTW, if your library produces the results posted for the numbers
posted, I would recomment using some different library... But the, you
haven't shown the whole test framework...
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Xavier Tarrago <Xavier.Tarrago@no_spam.cea.fr>
Date: 1997/09/23
Raw View
Rick Bielawski wrote:
>
> How can I prevent it from ignoring my setprecision(3) when the
> number being formatted happens to contain zeros to the right
> of the decimal point?

  You should use iomanip.
//-------------------------------------------------------------
#include <iostream.h>
#include <iomanip.h>

int main()
{
    static double dtab[] =
    {
        123.400,
        125,
        126.800
    };
    const unsigned int dtab_lgh = sizeof( dtab) / sizeof( dtab[0]);

    cout << setprecision( 9) << setiosflags( ios::showpoint);
    for( unsigned int i = 0; i < dtab_lgh; i++)
    {
        cout << dtab[i] << endl;
    }
}
//-------------------------------------------------------------
  output:
123.400000
125.000000
126.800000

  (HP Ansi C++)

  Hope it helps,
   Xavier
--
+--------------------------------------------------------------------------+
| X.Tarrago - mailto:Xavier.Tarrago@cea.fr
|  Commissariat a l'Energie Atomique |
|  Centre de Saclay - bat 611        | tel : 01 69 08 96 49
|  91191 Gif sur Yvette CEDEX        | fax : 01 69 08 75 97
|  France                            |
+------------------------------------+-------------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: haberg@matematik.su.se (Hans Aberg)
Date: 1997/09/23
Raw View
In article <01bcc5c3$4f167e20$3436e6cf@PCI-150.leadbelt.com>, "Rick
Bielawski" <rbielaws.leadbelt.com@home.com> wrote:
>while (N = NextNumber())
>  cout << setw(9) << setprecision(3) << (T += N) << "\n";
>
>With the following numbers: 123.4, 1.6, 1.8
...
>How can I prevent it from ignoring my setprecision(3) when the
>number being formatted happens to contain zeros to the right
>of the decimal point?

  Try
    cout << setw(9) << setprecision(3) << showpoint << (T += N) << "\n";

  See the book by Steve Teale, "C++ IOstreams handbook" for details. --
But some say the IOstreams standard has changed, and I do not know if
there is an up-to-date version of the book.

Hans Aberg <haberg@matematik.su.se>
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "John I. Moore, Jr." <70672.1744@compuserve.com>
Date: 1997/09/23
Raw View
Rick Bielawski <rbielaws.leadbelt.com@home.com> wrote in article
<01bcc5c3$4f167e20$3436e6cf@PCI-150.leadbelt.com>...
> I hope somebody can set me straight.  I get no response from the
> groups that appear to be more appropriate.
>
> Is there a way to make the following do what I think it ought to?
>
> while (N = NextNumber())
>   cout << setw(9) << setprecision(3) << (T += N) << "\n";
>
> With the following numbers: 123.4, 1.6, 1.8
> OUTPUT LOOKS LIKE THIS!
>
>     123.400
>         125
>     126.800
>
> How can I prevent it from ignoring my setprecision(3) when the
> number being formatted happens to contain zeros to the right
> of the decimal point?
> --

Use the manipulator showpoint.

_____________________________________________________________

John I. Moore, Jr.          phone:  (301) 924-0680
SoftMoore Consulting        email:  softmoore@compuserve.com
16233 Monty Court
Rockville, MD  20853-1344
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]