Code for making files

Feb 12, 2013 at 12:13pm
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
Feb 12, 2013 at 12:24pm
http://www.cplusplus.com/doc/tutorial/files/

contains everything you need to know
Feb 12, 2013 at 12:39pm
how do i write the results in the file though...
Feb 12, 2013 at 12:41pm
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.
Feb 12, 2013 at 12:48pm
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();
  }
Feb 12, 2013 at 12:48pm
I tried that first to see if it works, but it didnt
Feb 12, 2013 at 12:52pm
Are you sure?

Have you checked your project directories to see if the file has been created?
Feb 12, 2013 at 12:54pm
there was no txt file, so I tried creating one, but it still didnt work
Feb 12, 2013 at 1:03pm
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
Feb 12, 2013 at 1:08pm
Where was the .txt file, I looked in my documents, visual studio 2008, projects, Multiples2(name of my project), Debug,

There is no txt.
Feb 12, 2013 at 1:12pm
it worked, I had to release it first

Feb 12, 2013 at 1:14pm
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
Feb 12, 2013 at 1:18pm
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.
Feb 12, 2013 at 1:21pm
How do I make it write it in the same file each time
Feb 12, 2013 at 1:23pm
Get all of your user inputs
Open file
Enter while( y <= m ) loop
 -- Output data to file
Exit loop
Close file
Feb 12, 2013 at 1:30pm
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
Feb 12, 2013 at 1:34pm
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!";
}
Feb 12, 2013 at 1:35pm
i figured it out, thx
Feb 12, 2013 at 3:30pm
closed account (ihq4izwU)
That's nice.
Feb 13, 2013 at 3:07am
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.