Thread: Printing to file / Output issues

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    Question Printing to file / Output issues

    I'm trying to make a program that outputs every hour and minute of the day in

    hh:mm format and then print all of that to a text file.

    However, I have no idea how to make it go about printing it as

    00:00
    00:01
    00:02 and so on.

    Any help would be appreciated, as well as how to make it write every single timestamp to a text file.

    Thanks in advance.

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
      int n;
      int m;
      cin >> n;
          while (n>-2 && n<24)
          {
          cout << setfill('0') << setw(2) << ++n << "";
          }
      cin >> m;
          while (m>-2 && m<60)
          {
      cout << setfill('0') << setw(2) << ++m << "";
           }
      cout << n << ":" << m;
      system("PAUSE");
      EXIT_SUCCESS;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A for loop which counts the hours, and within that, a for loop which counts the minutes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I am new to c++ and a noob at it, could you help me out a little more than that, pretty please?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why?
    Since you spent less than a minute to reply rather than going away to look at some reference material to see if you could manage it yourself.

    You've already managed the hard part (IMO) of making cout output leading zeros.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Because I messed around with it a lot before coming to ask for help. I wouldn't come and ask for help if I had a clue.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    On a side note dont use system("pause"); system calls are unsafe.

    Use std::cin,get(); to freeze the console output. If your using std::cin >> as you are in this program depending on your compiler you may have to add std::cin.ignore() underneath you ignore the whitespace.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Ah, thank you kindly for the tip. I will use that from now on. =)

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So
    for ( m = 0 ; m < 10 ; m++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This is a real simple program, the basic outline is:
    • Open output file
    • for loop through the hours (0-23 inclusive)
      • for loop through the minutes (0-59 inclusive)
        • print hour/minutes in HH:MM format using cout
        • print same thing a above, but to file this time

    So, read up on for loops and nested loops along with some file I/O using fstream objects and you're done.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
        int hh;
        int mm;
        for ( mm = 0 ; mm < 59 ; mm++)
        for ( hh = 0 ; hh < 24 ; hh++)
        
        ofstream myfile;
        myfile.open ("TimeStamps.txt");
        cout.fill('0');
        myfile.fill('0');
        myfile << setw(2) << mm << ":" << hh;
        cout.fill(' ');
        myfile.fill(' ');
        myfile.close;
    Changed it up a little, but it gives me the error of:

    "14 C:\Dev-Cpp\Practice\Time1.cpp `myfile' undeclared (first use this function)"

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    ofstream myfile;
    >    myfile.open ("TimeStamps.txt");
    You need to move these lines outside the for-loop. Otherwise the scope of myfile is only within the loop. Also:
    Code:
    >    myfile.close;
    Should be:
    Code:
        myfile.close();

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Like this?

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
        int hh;
        int mm;
            
        ofstream myfile;
        myfile.open ("TimeStamps.txt");
        for ( mm = 0 ; mm < 59 ; mm++)
        for ( hh = 0 ; hh < 24 ; hh++)
    
        cout.fill('0');
        myfile.fill('0');
        myfile << setw(2) << hh << ":" << mm;
        cout.fill(' ');
        myfile.fill(' ');
        myfile.close();
    It's only outputting this: "24:59"
    Not sure what is wrong with it now.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It's only outputting this: "24:59"
    This is true. A for-loop only executes a single statement, unless you include braces to indicate otherwise. This is one reason it's important to properly indent one's code.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your loops need braces around the code that is supposed to be in the loop. Otherwise, only the next statement will be a part of that loop. In this case, that means that your loops only execute the cout.fill('0'); line over and over again 1500 times, then execute the code after it once.

    BTW, your first loop should probably stop at 60, not 59.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I have no idea how to go about fixing that. Could you point me into the right direction or something? And I didn't think the indents were a big deal on small code, usually pretty easy to follow, no?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM