April 23rd, 2012, 06:41 PM
Ask user for output file name
I am trying to create a program, where I prompt the user to name the output file name. I need the output file to be a .txt file. I know how to create, open and close an output file. But when I try to implement by asking user for the name, I am stuck. In addition, I am trying to let the user print out an entire sentence, but I can only get the first word. How would I fix this problem?
Another part of the program I am trying to solve is:
If the user chooses to open an existing file, it should show a list of all files that have been created with that program. The user types up the name of the file, and the program displays the contents of the file. Can anyone give me ideas of how I approach this problem.
using namespace std;
int main (void)
//Part 1: create new file for user
ofstream outfile; // object for writing to a file
cout << "Enter the name of the file you wish to create: " << endl;
cin >> filename;
outfile.open (filename + ".txt"); //trying to make a .txt file
cout << "Enter a sentence: " << endl;
cin >> sentence;
outfile << sentence <<endl; // this line prints the first word not the entire sentence
outfile << "hello" << endl;
//Part 2: User can open existing file
Last edited by captjack; April 24th, 2012 at 02:19 AM.
Tags for this Thread
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!