how do i change the output file according to the input from the user?
Ex:
user enter "test"
how do i make the rest of the program to be out puted to test.txt
or if he entered "exam"
to exam.txt
and so on.
how do i change the output file according to the input from the user?
Ex:
user enter "test"
how do i make the rest of the program to be out puted to test.txt
or if he entered "exam"
to exam.txt
and so on.
if i understand the question correctly, you want to scan the user's input, and then create and open the file using the input you just scanned.
so the logic of your program should ask the user for the file name, check if the name entered is valid and can be used, open a file with this name and work with this file. so based on what name the user is going to enter, your program will create the file with a appropriate name.
1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!
I had a similiar problem. I made a RPG, and I have the username as the filename and the password as the first line of the file. The other lines are experience counters. I can add the exp at any time. I had to use a struct for all the variables. Use a unique filename for each you say? Anyways I hope this helps.....
FloYd
This worked for me using MSVC++ 6.0:
Code:#include <fstream> #include <iostream> #include <string> using namespace std; int main() { cout << "Type in filename: "; string strFName; cin >> strFName; strFName += ".Txt"; cout << "Creating output file: " << strFName << endl; ofstream OutputFile( strFName.c_str() ); return 0; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens