Code for making files

Hello, I want to make my program make a file and write results in them. My program finds the multiples of x under y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

void main()
{
	int x = 0, y = 0, m = 0;
cout << "Hello, this program will find the multiples of x below y" << endl;
system("pause");
cout << "Please enter the value of x: " ;
cin >> x;
cout << "Please enter the value of y: " ;
cin >> m;
cout << "Now the computer will find all the multiples of x below y" << endl;
system("pause");
while(y <= m)
{
y += x;
cout << y << endl;
}
system("pause");
}

Help please
http://www.cplusplus.com/doc/tutorial/files/

contains everything you need to know
how do i write the results in the file though...
Use a ofstream object.

The very first piece of code in the link that Darkmaster gave is an example of how to write out to a file.
i tried that, it doesn't work, i put it right under the cout << y << endl;
1
2
3
4
5
6
7
ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
I tried that first to see if it works, but it didnt
Are you sure?

Have you checked your project directories to see if the file has been created?
there was no txt file, so I tried creating one, but it still didnt work
This worked for me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>

int main( int argc, char* argv[] )
{
  std::ofstream outFile( "example.txt" );

  if( outFile.is_open() )
  {
    outFile << "This is some text\n";
    outFile << "This is more text\n";
    outFile.close();
  }

  return 0;
}


http://s18.postimage.org/h5pvm86eh/txttest.png
Where was the .txt file, I looked in my documents, visual studio 2008, projects, Multiples2(name of my project), Debug,

There is no txt.
it worked, I had to release it first

i put in outFile << y;
I tried to do it so it will right every single result, but it only did the last one, help
You're not opening a new file for each result are you? With the same name?

It'll overwrite the previous one.

That's what it sounds like it's doing.
How do I make it write it in the same file each time
Get all of your user inputs
Open file
Enter while( y <= m ) loop
 -- Output data to file
Exit loop
Close file
How do I close file and is the output data to file part is the code that you used except i replace the "This is some text" with y
closed account (ihq4izwU)
The only thing I doubt about my source code is that I don't get what you mean about multiples of x below y. So I just assume that if x = 10 and y = 2, then the results would be the y multiples of x which is 2 multiples of 10 as such: 10, 20.

Thats all, anyways just see it for yourself! =)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include<fstream>
#include <windows.h>
using namespace std;

int main()
{
	ofstream output;
	int x = 0, y = 0, m = 1;
	cout << "Hello, this program will find the multiples of x below y" << endl;

	cout << "Please enter the value of x: " ;
	cin >> x;
	cout << "Please enter the value of y: " ;
	cin >> y;
	cout << "Now the computer will find all the multiples of x below y" << endl;
	system("pause");
	output.open("1.doc");
	while(m <= y)
	{
	output<< x * m << endl;
	m++;
	}
	output.close();
	cout<<"Saving results to '1.doc' in the current directory..."<<endl;
	system("pause");
	cout<<"SUCCESSFUL!";
}
i figured it out, thx
closed account (ihq4izwU)
That's nice.
my program finds the multiples of x below y, that means if you were to write x as 3 and y as 1000, my program will find the multiples of 3 until it reaches 1000
Topic archived. No new replies allowed.