This is my code to create a new file below.
The main problem is that the program does not halt for entering file name
and also the program creates a file .txt and doesn't save anything in it.
Please Help

int newfile()
{
    
    system("cls");
    cout <<"\t\t-------------------------------------" <<endl;
    cout <<"\t\t|              NEW FILE             |" <<endl;
    cout <<"\t\t-------------------------------------" <<endl;  
    cout <<"\t\t|Enter New File Name| "; getline(cin,createfile);
    cout <<"\t\t----------" <<endl;
    
       
    string text;
    int nl = 0;

    cout<<"Press enter twice in a row to quit: ";

    while (nl < 2 && getline(cin, text))
    {
        if (text.empty())
        {
            ++nl;
        }
    }
       
    if(! outfile)
    {
         cout <<"error writing to file" <<endl;
         return -1;
    }
    outfile.open((createfile+".txt").c_str());
    outfile << text <<endl;
    outfile.close();
    
}

Any help appreciated.
Anurag.

'outfile' doesn't seem to be defined.

This code works fine for me:

#include <iostream>

using namespace std;

int main()
{
    string text;
    int nl = 0;

    cout<<"Press enter twice in a row to quit: ";

    while (nl < 2 && getline(cin, text))
    {
        if (text.empty())
        {
            ++nl;
        }
        cout << text << endl;
    }
       
    return 0;
}

Dave

hey, now could you please tell me how to export "text" to a .txt file..
Any Help Appreciated.

Anurag.

Look into fstream/ofstream

Dave

hey according to your code, the cout << text << endl;
posts a blank line since we press enter twice.
I would suggest to use a better function such as press f5 to finish
Then No probs right?

Again Thanks
Anurag

Is there any keypress function as of such
I triet kbhit() but it seems to be useless.
Same case with_getch();

Anurag

commented: Please stop rapid fire posting. Also, there's no need to ask the same question in multiple threads. It only annoys the people helping you. -4

If you check your other thread I've already told you what the problem is. For the sake of argument, I'll repeat myself.

Calling getline is overwriting your text variable with each loop. Therefore, when you press enter twice, text contains the character '32' (Which represents Enter)

When you get your data into text, you need to store it somewhere that it won't be overwritten.

May I suggest a new thread titled "How do you catch F5 key?"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.