Printing to file / Output issues

This is a discussion on Printing to file / Output issues within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved Your loops need braces around the code that is supposed to be in the loop. Otherwise, ...

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by Daved View Post
    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.
    It's supposed to be a clock, for some reason if you do it up to 60, it includes 60. at 60 mm it technically goes up an hour.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It only includes 60 because of your other problem of not adding the braces. Notice how it outputs 24:59, but there isn't supposed to be a 24 hour either.

    To fix your problem, just add braces around the code that belongs in the loop. The first step is to identify which code belongs in the loop. Can you identify the lines of code you think need to be repeated each time and therefore belong in the loop?

    The syntax of a for loop is:
    Code:
    for (blah; blah; blah)
    {
        // code goes here
        // and repeats
        // each time in the loop.
    }
    You are missing the blue things.

  3. #18
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Personally I think this when you tell me that:

    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();
    But I think that only does one of the for loops, aye?

    edit: that fills my text file with a huge amount of ''''''''''''''s
    Last edited by Roflcopter; 10-11-2007 at 06:23 PM.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That looks good to me for the inner loop.

    You would normally add the braces for each for loop separately. You want the outer for loop to contain the entire inner for loop, so you would add the starting brace before the second for statement (the start of the inner loop) and the closing brace after the closing brace of the inner loop (which is the end of the inner loop). In this case it actually isn't needed, since that whole block is counted as one block and is all repeated anyway, but for clarity and good practice you should add those braces.

    As for the output in the file, that is really weird. It happens for me when I use notepad to open it, but not when I open it with another editor. Try adding a newline or space after the minute is output, then you should be able to see it better.

  5. #20
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    I'm going to follow what seems to be forum etiquette and not flat out do it for you, but I will fix your tab problem, then you should be able to see the problem easier:

    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();
    	...

Page 2 of 2 FirstFirst 12
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-22-2008, 12:19 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04: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, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21