CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012

    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.


    #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; (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;

    //Part 2: User can open existing file

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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003

    Re: Ask user for output file name

    Your while loop doesn't accomplish anything. What are you trying to do there?

  3. #3
    Join Date
    Feb 2012

    Re: Ask user for output file name

    I put it in a while loop, because I have more parts to program that I haven't started yet. I will be asking the user to open and close other files. So that's why it seems sort of pointless at this moment.

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