Thread: Problem with formatting output

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Problem with formatting output

    I have been trying to format output so that it gives two decimal accuracy when it is displayed. The problem I am having is that the "fixed" manipulator for output creates an error.

    Code:
      #include<iostream.h>
    #include<iomanip.h>
    //using namespace std;
    
    
    int main()
    {
    
    	cout << "The center of gravity for the loaded plane is " << setiosflags(ios::fixed) << setprecision(2) << CG_filled << " inches from the nose of the aircraft." << endl;
    
    	return 0;
    }
    I was searching around and found the "setiosflags(ios::fixed)" and it works, but I don't know how it works. If I remove the .h from the pre-processor directives and remove the remark symbol before the namespace statement it will allow me to place just "fixed" in the cout statement. If someone could help me understand why I need to use the namespace std or what the setiosflags does, that would be greatly appreciated.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    <xxxx.h> was the old way for standard header files. The new header files drop the 'h'. Like so:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
      cout << "Hello world!" << endl;
      return 0;
    }
    The old ".h" way has been deprecated, so although it is still possible to use that now, that won't always be the case in the future. What you should do is use the new libraries.

    Now as for that whole std thingie, that's a namespace. In the new and improved libraries, all of the identifiers have been placed in a namespace called std to avoid potential name conflicts. The easiest way to ensure that you are using the whole std namespace is as shown above. There are other ways, however, that some people will argue are better/worse.
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    int main()
    {
      cout << "Hello World!" << endl;
      return 0;
    }
    /* OR */
    #include <iostream>
    int main()
    {
      std::cout << "Hello World!" << std::endl;
      return 0;
    }
    If you feel the need to know exactly what namespaces are, a quick search will probably yield many answers.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    <iostream.h> and <iomanip.h> are not deprecated, they're simply non-standard. It's the older C-library headers like <stdio.h> and <stdlib.h> that are deprecated, in favour of <cstdio> and <cstdlib> and the like, but they're still standard.

    The distinction is important; any conforming compiler (for the current version of the C++ standard at least) is required to provide <stdio.h>, <stdlib.h> etc. so you can rely on them being available. They're not required to provide <iostream.h>, <iomanip.h> etc (and not all compilers do).

  4. #4
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Even so, they'd better do so, just for compatibility with older code. Besides, it does not cost them much. So one can consider that these headers are present where the standard ones are, in my opinion.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ i don't think so... I think it's better just to tweak your code to make it more standards-compliant, because if you don't, eventually they may reach a point where backward-compatability may not be practical anymore, then all the programs you still have conforming to pre-standard days will need ALOT of tweaking...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by lyx
    Even so, they'd better do so, just for compatibility with older code. Besides, it does not cost them much. So one can consider that these headers are present where the standard ones are, in my opinion.
    The C++ standard came out in 1998; we've already been through a period of 5 years where those older versions were retained for reasons of backwards compatibility. Compilers are now starting to drop the older headers. You can consider them to be present if you wish, but your next compiler may not ;-)

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thanks for the help. I didn't realize that they were actually different libraries. I thought it was just a naming convention. Could someone please explain what the setiosflags and ios::fixed do to the output. It has been a while since I have done any programming, so I remember that :: is a scope operator, but I can't remember what that does. I might not have even learned it in high school to begin with, but it is something i'd like to learn. Thanks again to all who contributed.

  8. #8
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I was just saying that you can consider that, to my mind. I wasn't saying to code non-standard when you can do so.
    Actually, I don't see the point of deleting them, it does not bring anything good to compilers...

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. array sort output problem
    By radiantarchon28 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2007, 01:49 AM
  3. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  4. formatting output in C++
    By ubershatten in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 10:15 AM
  5. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM