CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Feb 2012
    Posts
    9

    Question 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.

    Thanks!


    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    int main (void)
    {
    //Part 1: create new file for user

    string name;
    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

    while (outfile.is_open())
    {
    char sentence[100];
    cout << "Enter a sentence: " << endl;
    cin >> sentence;
    outfile << sentence <<endl; // this line prints the first word not the entire sentence
    outfile << "hello" << endl;
    outfile.close();
    }

    //Part 2: User can open existing file

    return 0;
    }
    Last edited by captjack; April 24th, 2012 at 01:19 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured