Code for making files

Code Apperentice (34)
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
Darkmaster (494)
http://www.cplusplus.com/doc/tutorial/files/

contains everything you need to know
Code Apperentice (34)
how do i write the results in the file though...
iHutch105 (1092)
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.
Code Apperentice (34)
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();
  }
Code Apperentice (34)
I tried that first to see if it works, but it didnt
iHutch105 (1092)
Are you sure?

Have you checked your project directories to see if the file has been created?
Code Apperentice (34)
there was no txt file, so I tried creating one, but it still didnt work
iHutch105 (1092)
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
Code Apperentice (34)
Where was the .txt file, I looked in my documents, visual studio 2008, projects, Multiples2(name of my project), Debug,

There is no txt.
Code Apperentice (34)
it worked, I had to release it first

Code Apperentice (34)
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
iHutch105 (1092)
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.
Code Apperentice (34)
How do I make it write it in the same file each time
iHutch105 (1092)
Get all of your user inputs
Open file
Enter while( y <= m ) loop
 -- Output data to file
Exit loop
Close file
Code Apperentice (34)
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
mjjm08910 (7)
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!";
}
Code Apperentice (34)
i figured it out, thx
mjjm08910 (7)
That's nice.
Registered users can post here. Sign in or register to post.