Unless the output file is created with the correct name, its content is wrong

I expected the line inside the file, to have the name of the file, but instead the line contains the string that was previously assigned to the string variable used for giving name to the file.
For example, if the name asigned to the file is "Text8.txt" i expected the first line in it was:
Hi World, my name is:Text8.txt
but instead it has the line with text i previously typed.

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
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
string		outName="E:/FakeOld.txt", newName="E:/FakeNew.txt";
ofstream 	outFile;
void 		dtaToSrt();
void 		outFWho();
  
int main() {
	dtaToSrt  ();					// will process some data creat some file
	system ("pause");
	return 0; 
}
void dtaToSrt(){
	outFWho  ();		    		// select a name for outFile ... 
	 
//	cout    <<"Hi World, my name is:"<<outName<<endl;
	outFile <<"Hi World, my name is:"<<outName<<endl;
	outFile.close();
}
void outFWho() {
	cout<<"Give me outFile name:"<<endl;
	getline(cin, outName); // Si 
	cout<<"Please verify...:"<<outName<<endl;
	
	cout<<"Try  another one:";
	getline(cin, newName);
	 
	string outName=newName;
	cout<<"       Confirmed:"<<outName<<endl; 
//  outFile.open(outName,ios::in);
    outFile.open(outName.c_str());
    if(outFile.fail()) {
		cout<<"A problem with:"<<outName<<endl;
	}
}
Last edited on
On line 30 you're creating a local named 'outName' that hides the global named 'outName', so the value of the global is only updated on lie 24.
Marvelous!. Solved!
i've been having problems with handling string(string) and arrays(char).

Topic archived. No new replies allowed.