Jump to content

formatting the output

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

In the code below I have used setw(10) which means field width is 10. Does decimal point also constitute part of field width? If it does, then "1234567.00" is 10 characters long.

Why is there an error in the output?

How can I reset the output to normal?


#include <iostream>

#include <cstdlib>

#include <iomanip>


using namespace std;


int main()

{

    float fpn;


    cout << "enter the number: ";

    cin >> fpn;


    cout << setiosflags(ios::fixed)     //fixed (not exponential)

    << setiosflags(ios::showpoint)      //always show decimal point

    << setprecision(2)                  //two decimal places

    << setw(10)                         //field width 10

    << fpn << endl;                     //finally, the number


    cout << "enter the number again: ";

    cin >> fpn;


    cout << fpn << endl << endl;


    system("pause");

    return 0;

}


OUTPUT:

enter the number: 123456789

1234567[COLOR="#FF0000"]92[/COLOR].00 // [B]I didn't enter this value[/B]

enter the number again: 2

2.00 // [B]how do I get normal "2" again?[/B]


Press any key to continue . . .


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello Jackson,

Jackson said:

123456792.00 // I didn't enter this value
The IEEE 754 decimal32 float type that it uses can only represent up to seven distinct numbers, unless it is a fraction. Generally whole numbers are better left to the integer type, as there are larger types available to store your number.

You can however use a double type to overcome this limit, as it is sixty-four bits in length allowing a fifty-two bit fraction to store your longer number.

Jackson said:

2.00 // how do I get normal "2" again?
You have set the precision to be two, and the point to be shown (ios::showpoint), so naturally you will need to turn those off before you wish to print a single "2" with no precision or radix point.

Try to see if you can do this and report back on your success,

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi Alexander

It's good to see your replies. :)

Alexander said:

The IEEE 754 decimal32 float type that it uses can only represent up to seven distinct numbers, unless it is a fraction. Generally whole numbers are better left to the integer type, as there are larger types available to store your number.

You can however use a double type to overcome this limit, as it is sixty-four bits in length allowing a fifty-two bit fraction to store your longer number.

I have used double for variable fpn. Now it seems setw(10) isn't working. It should only show 10 characters but have a look on the output:


enter the number: 1234567899887777

1234567899887777.00 // [COLOR="#0000CD"]there are 19 characters including the decimal point[/COLOR]

enter the number again: 3

3.00


Press any key to continue . . .


Alexander said:

You have set the precision to be two, and the point to be shown (ios::showpoint), so naturally you will need to turn those off before you wish to print a single "2" with no precision or radix point.

Try to see if you can do this and report back on your success,

Unsuccessful! :) I need to use resetiosflags(). I don't know which arguments to use with it. I googled it and everything was over the head! Please help me if possible. Thanks a lot.

Best wishes
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I apologise for the delay, for each ios flag you set, you should pass the same flag to resetiosflags. Each flag is a bit mask, so the reset function will revert the bits set by those specific flags.

Try to see if it works, and post the code you attempt.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users