creating a file with user defined file name and saving to it

hello there, im new to this site and ive been looking for ages to try and find a way of doing it, what i need to do is create a text file that the user will name and it will then write the data to it, ive tried numerous ways and the best ive come up with is writing to the file but i cannot get it so the user enters the file name
any help will be greatly appreciated, cheers!


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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include<iostream>
#include <Windows.h>
#include <stdio.h>
using namespace std;

char decision;

int main()


//insert code to allow user to enter file name

do 
{

//user inserts number of points they wish to be plotted

	cout<<"\n Please enter the number for the amount of sample points ";
	cout<<"\n (the number has to be odd and has to be between 3 and 31) : \n\n" ;
	cout<<"\n Output waveform for (number) sample points: ";
	cout<<"\n 


//ask user if they would like to re run the program
	cout<<"\n Would you like to re-run the program? y/n :\n";
	cin>> decision;
	

}

	
	while (decision == 'y' || decision == 'Y');


	if (decision == 'n' || decision == 'N')
			{
				//end program and message
				



system ("Pause");
return 0;
} 
Have you researched c++ IO streams and how to write a file?
Ive managed to write a file to a set file name by using this :

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<iostream>
#include <Windows.h>
#include <stdio.h>
#include <fstream>

using namespace std;

char decision;

int main()
{


//insert code to allow user to enter file name

ofstream programdata;
	outputFile.open("programdata.txt");

do 
{


//user inserts number of points they wish to be plotted

	cout<<"\n Please enter the number for the amount of sample points ";
	cout<<"\n (the number has to be odd and has to be between 3 and 31) : \n\n" ;
	cout<<"\n Output waveform for (number) sample points: ";
	




//ask user if they would like to re run the program
	cout<<"\n Would you like to re-run the program? y/n :\n";
	cin>> decision;
	

}

	
	while (decision == 'y' || decision == 'Y');


	if (decision == 'n' || decision == 'N')
			{
				//end program and message
			}

	outputFile.close();


system ("Pause");
return 0;
}



and it worked the first time i did it but now it says

Error: identifier "outputFile" is undefined
and i also need to be able to change the name of the file i tried setting it so that the name of the variable would be from a string but i couldn't get that to work either


im getting really stuck with it as i get so far and then i could enter the name of the file but then it didnt actually create a file and then when i ran the program again it bypassed all user inputs and closed the program
thankyou for the quick reply :)

The variable is called programdata but you use it as outputFile.
woops my mistake aha sorry about that, so how can i go about changing the name of the file then? am i going in the right direction by using a string that takes the user input + .txt and outputting that to programdata.open ?
Topic archived. No new replies allowed.