How can i have the output in double digits

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lauramcm
    New Member
    • Mar 2007
    • 4

    How can i have the output in double digits

    Hey... im new to c++ and im trying to write a program but am having difficulty trying to figure out how i can make my output have double digets even when it is 8 eg be 08.

    i think its got somthing to do with a combination of setw, setfill and setprecision.

    Please Help someone!

    Laura
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Lauramcm
    Hey... im new to c++ and im trying to write a program but am having difficulty trying to figure out how i can make my output have double digets even when it is 8 eg be 08.

    i think its got somthing to do with a combination of setw, setfill and setprecision.

    Please Help someone!

    Laura
    try
    Code:
        int i=8;
        cout << setfill('0') << setw(2) << i << endl;

    Comment

    • Lauramcm
      New Member
      • Mar 2007
      • 4

      #3
      what about if it is for calculating time... say for instance


      i get this...

      3:4:24.2

      but i need 03:04:24.2?

      sorry im really bad atm.. iv only started learning!

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Lauramcm
        what about if it is for calculating time... say for instance


        i get this...

        3:4:24.2

        but i need 03:04:24.2?

        sorry im really bad atm.. iv only started learning!
        once you setfill() it is fixed until you change it, you have to use setw() before each variable to be printed.

        Comment

        • Lauramcm
          New Member
          • Mar 2007
          • 4

          #5
          ok that work... but it doesnt when i have a decimal!

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by Lauramcm
            ok that work... but it doesnt when i have a decimal!
            give an example of your problem?

            Comment

            • Lauramcm
              New Member
              • Mar 2007
              • 4

              #7
              speeds: 3.9 38.2 18.6

              distances: 0.4 10 2.5


              time: 00:06:09.2

              time : 00:15:42.4

              time : 00:08:03.9

              Total time : 00:29:55.5

              but i dont know how to get the last digets to be 09.2 or 03.9

              Comment

              • horace1
                Recognized Expert Top Contributor
                • Nov 2006
                • 1510

                #8
                try
                Code:
                    float x=9.2;
                    cout << setfill('0') << setw(4) << x << endl;

                Comment

                Working...