#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

int main(void)
{
	for(int COUNT = 0; COUNT <= 1000; COUNT++)
	{
		if(COUNT > 1000)
		{
			break;
		}
		if(COUNT <= 1000)
		{
			std::string TO_STRING;
			std::stringstream OUTPUT;
			OUTPUT << COUNT;
			TO_STRING = OUTPUT.str();

			ofstream myfile;
			FILE.open ("data.txt");
			FILE << TO_STRING << "^" << TO_STRING << "+" << endl;
			FILE.close();
		}
	}
	cin.get();
	return 0;
}

I have a loop that runs until the specified count in reached, and in this loop, the string prints just fine to the specified text file. However, I'm a bit confused as to how I can print each string that is created each time the loop runs. The file only contains the output from the last loop run. How can I have each created string be printed onto a new line until the threshold is met?

- xtremerocker

Something like FILE.open ("data.txt",ios::app); So you append, rather than rewrite.

Also, opening the file before the loop, then closing it when you're done would help.

Your two if statements are always false and true in that for loop.

Unfortunately, it was a no go. Now it prints only the first value into the text document and nothing else. However, the loop is working just fine, because I added a check (which is marked by the comment section in the code) which displays each integer between 1 and 1000.

Here's what I have now:

#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

int main(void)
{

	remove("data.txt");

	ofstream FILE;

	for(int COUNT = 1; COUNT <= 1000; COUNT++)
	{

			std::string TO_STRING;
			std::stringstream OUTPUT;
			OUTPUT << COUNT;
			TO_STRING = OUTPUT.str();

			FILE.open ("data.txt", ios::app);
			FILE << TO_STRING << "^" << TO_STRING << "+" << endl;
			
			// check which displays that the for loop is running
			// and that COUNT is rising in increments of one
			cout << "CHECK: " << TO_STRING << endl;
	}

	FILE.close();

	cin.get();
	return 0;
}
...
FILE.open ("data.txt", ios::app);
FILE << TO_STRING << "^" << TO_STRING << "+" << endl;
OUTPUT.clear();
...

Like that? Because I ran that and nothing changed.

OK lets goover what i've done.

#include <sstream>
#include <fstream>

int main(int argc, char *argv[])
{
	std::ofstream outfile("data.txt", std::ios::in);
                std::ostringstream output;

	for(int count = 1; count <= 1000; count++)
	{
            output.str("");
            output << count;
            outfile << output.str() << "^" << output.str() << "+" << std::endl;
	}

	outfile.close();
	return 0;
}

Sorry, you didn't want clear(), it was str("") you need. Anyway you can see i've made a few changes, such as removing un-needed headers, taken out namespace std; .
Dropped your capitals name convention, 'cause it's horrible.

Then I went and decided that you don't even need stringstreams and I'm not sure why you are using them.

#include <fstream>

int main(int argc, char *argv[])
{
	std::ofstream outfile("data.txt", std::ios::in);

	for(int count = 1; count <= 1000; count++)
		outfile << count << "^" << count << "+" << std::endl;

	outfile.close();
	return 0;
}

Chris

I'd like to follow up on Chris' post: std::ofstream outfile("data.txt", std::ios::in); I would personally go for std::ofstream outfile("data.txt", std::ios::out); But more important: One of your next steps should be to check is the file did indeed open or not. If the open command fails (and it will if the file already exists for example), you want the user and the program to know that you could use if (outfile.good()) for that.

Comments
Duh, talk about retards mistake. Seems we picking up each others stupidity lately :)

Thanks for all the replies. I used the code that Chris provided and implemented the check to see if the file is good or not, suggested by niek_e.

Thanks again!

- Michael

This question has already been answered. Start a new discussion instead.