How to allow user to name text file that is created c++
<p>I am trying to create a text file that the user inputs the name for. It should just create the text file and then close it, and then give feedback through the terminal if it was created successfully or not. I was able to figure out how to create the file if I hardcoded the file name but now that I am trying it with user input I can't get the file to be created. Right now it doesn't even let me type anything in, it just ends when i call the function. Thank you for your help. </p>

<p>EDIT: I am not sure if it's because my getline call is just getting a blank line then ending or why it won't let me cin anything.</p>

<p>EEDIT: This is my full code,I guess the part I thought wasn't working is, so hopefully somebody can tell me why my code just ends after i select 1 for my switch case. Obviously my code isn't complete, but this is everything I have.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;stdio.h&gt;

using namespace std;

void createDB() {
    ofstream db;
    string filename;
    cout &lt;&lt; "Enter the name of the database you want to create: \n";
    getline (cin, filename);

    db.open(filename.c_str());
    if(!db) { // checking if the file could be opened
        cout &lt;&lt; "\nCould not create database\n";
    }// add more checks to make sure file doesn't have same name
    else {
        cout &lt;&lt; "\nYour database " &lt;&lt; filename &lt;&lt; " was created successfully\n";
    }

    db.close();

}

void openDB() { // just opens hard coded file for now
    // need to add check to see if one is already open
    cout &lt;&lt; "Enter the name of the database you want to open: ";
    string line;

    //ifstream file (
}

void closeDB() {

}

void display() {

}

void update() {

}

void create() {

}

void add() {

}

void del() {

}

int menu() {
    cout &lt;&lt; "Enter the number of the operation you wish to perform (1-9)\n"
    &lt;&lt; "1. Create new database\n"
    &lt;&lt; "2. Open database\n"
    &lt;&lt; "3. Close database\n"
    &lt;&lt; "4. Display record\n"
    &lt;&lt; "5. Update record\n"
    &lt;&lt; "6. Create report\n"
    &lt;&lt; "7. Add a record\n"
    &lt;&lt; "8. Delete a record\n"
    &lt;&lt; "9. Quit\n";

    int sel = 0;
    cin &gt;&gt; sel;

    switch (sel) {
        case 1: createDB();
            //menu(); // after creating file go back to list of options
            break;

        case 2: openDB();
            break;

        case 3: closeDB();
            break;

        case 4: display();
            break;

        case 5: update();
            break;

        case 6: create();
            break;

        case 7: add();
            break;

        case 8: del();
            break;

        case 9: return 0;
            break;

        default: cout &lt;&lt; "Please try again and enter a valid number\n\n";
            menu();
            break;
    }
    return true; // to avoid error saying control may reach end of non-void function
}


int main() {
    menu();

    return 0;
}
</code></pre>