Hey Guys,
Recently ive been developing a text editor when i came to stop.

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;
    
       
    getline(cin,text);

here getline accepts code only until one press enter key
How to enable the same option when enter key is pressed twice?
Please Immediately Help.

Thanks beforehand.
Anurag

There are a number of ways to do it. Here's one:

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text;
    int nl = 0;

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

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

Can you please explain the code using comments //.....
I'm sorry, but I'm just a beginner in C++

Oh i understood thx

There are a number of ways to do it. Here's one:

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string text;
    int nl = 0;

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

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

Hey Narue... Your code runs fine but when i want to save the input using getline function
it doesnt.....


Here's My code..

cout<<"Press Enter Twice When You're Done"<<endl;
cout<<endl;
cout<<endl;
while (nl < 2 && getline(cin, text))
    {
        if (text.empty())
        {
            ++nl;
        }
        
    }
cout<<endl;	
cout<<endl;
char filename[20];
cout<<"Specify File Name:";
cin>>filename;
if(filename!=NULL)
{
ofstream go;
go.open(filename);
go<<text;
}

Please mention any errors. Constructive suggestions will be appreciated.

Cheers,
Anurag

You have to add an else statement after line 9

if (text.empty())
{
     ++nl;
}
else
{
     // to save, assign text to a variable here 
}

It is because the text will be overwritten in each iteration at line 4. Logically, when user presses enter twice, the last captured value in text would be empty. Thus, the value you get for your variable 'text' at line 21 is empty.

I didn't understand by "to save assign text to a variable."
Please explain
Beg your pardon

Anurag

while (nl < 2 && getline(cin, text)) If you don't understand what happens here, you are getting the data that you have just typed in and assigning it to the "text" variable. You will continue to do this while nl is less than 2.

So when you type in "Bob" and press enter, "Bob" is assigned to text.
Then on the next iteration, you just press enter. This causes getline to overwrite your text variable with nothing. text is now empty again and the text.empty() is true so nl is incremented by 1.

You need to move the data you entered somewhere so that it isn't overwritten.

So that means,
now i should say,

else{
text = bob
cout<<"Specify File Name:";
cin>>filename;
if(filename!=NULL)
{
ofstream go;
go.open(filename);
go<<bob;
}
}

No you got the assignment the wrong way around.

If you still want to use getline(cin, text) Then you would need to then use bob = text;

It ain't workin bro... It skips the part

Here's the code
Please look into it.

int newfile()
{
system("CLS");
char date [9]; char time [9];
_strdate(date); 
_strtime(time); 
cout<<"--------------------CREATE A NEW FILE--------------------------"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<"Date:"<<date<<"-----------------------------------Time:"<<time<<endl;
cout<<"---------------------------------------------------------------"<<endl;
cout<<"Press Enter Twice When You're Done"<<endl;
cout<<endl;
cout<<endl;
while (nl < 2 && getline(cin, text))
    {
        if (text.empty())
        {
            ++nl;
        }
        else
        {   
            char filename[20];
            string bob;
            bob  = text;
            cout<<"Specify File Name:";
            cin>>filename;
            if(filename!=NULL)
                              {
                              ofstream go;
                              go.open(filename, ios::out);
                              go<<bob;
                              }
            }
            
    
cout<<endl;	
cout<<endl;

system("CLS");
cout<<"TO END THIS PROGRAM ";
system("pause");


}
}

It jumps to
:TO END THIS PROGRAM

It's not skipping, but it's printing out exactly like you asked it to.

You might want to check where stuff is in your while loop.

Please post the correct code
Or atleast correct my code

Thanks till now
Anurag

If i mod the while loop, the press enter twice doesn't work

Paste the code you have, then write out in English what you think this code is doing, line by line.

This will help you understand where and what the problem is.

Or suggest alternate method

No, I'm not here to do your work for you. If you're not going to help yourself, then why should I?

The error you're having is pretty simple. Stepping through it yourself and thinking about what is happening will lead you to the correct solution.

Heck with the error.
I'm unable to find it.
Hint please...

Heck with the error.
I'm unable to find it.
Hint please...

How's this for a hint? Start over and work through your program slowly. Write test programs to teach yourself how things work, then use that knowledge to incorporate functions and constructs into your program.

In this thread I'm getting a distinct sense of helplessness and laziness. Probably the biggest reason you're not encouraging people to help is you're completely failing to help yourself first and you're not making the most of the help given.

Please I'm Desperate
I have 2 days more.....
Please

commented: Failed to read any of the replies and links -2

....
Allright i'll do it...
Thanks for the advice

Anurag

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

Anurag

I triet kbhit() but it seems to be useless.
Same case with_getch();

How did you try them? Is it more likely that they're actually useless, or you simply didn't use them correctly?

here
i use kbhit() = 68;
and it says non lvalue error

kbhit is a function. It returns a boolean value (in the form of an int) telling you whether there's a keypress waiting in the keyboard buffer. You use it like this:

if (kbhit())
{
    // A key was pressed
}
else
{
    // No key presses waiting
}

i want the program to react when a key is pressed

Anurag

Its all right...


Better Use A Delimiter with getline!!!

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.