File i/o Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • daduke40
    New Member
    • Mar 2008
    • 9

    File i/o Question

    I am grabbing a time slot from a text file

    ---text file--

    11:01 am.

    When I want to grab the minutes column, it only grabs the '1' instead of the '01'. I have the variable set to int. Any suggestions?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Why do you need the tens place? Why not use a Date class, then you can just use some sort of Date.getMinutes () function?

    Can you add it yourself? This is a QAD, but very dirty (using a getMinutes() function or something close would be a much, much better idea, lest you end up on The Daily WTF) .

    [code=cpp]
    if (i_min < 10 && i_min >= 0)
    cout << '0' << i_min;
    [/code]

    Comment

    • daduke40
      New Member
      • Mar 2008
      • 9

      #3
      Originally posted by sicarie
      Why do you need the tens place? Why not use a Date class, then you can just use some sort of Date.getMinutes () function?

      Can you add it yourself? This is a QAD, but very dirty (using a getMinutes() function or something close would be a much, much better idea, lest you end up on The Daily WTF) .

      [code=cpp]
      if (i_min < 10 && i_min >= 0)
      cout << '0' << i_min;
      [/code]
      Well, its something I havent learned yet, but why won't the program take 01? And is there any other way besides the if statement to add that on?

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        I'm guessing you're taking it as an int, right? Well, numbers are stored in binary, so what the computer sees is:

        0000 0001

        It can't tell the difference between 01 and 1, so it prints 1. However, if you use a date class, I'm guessing that they will come as objects, and print out as '01' or '0x'.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Read up on how to use the cout.fill() member function.

          Comment

          Working...