Exit C++ program by pressing the enter key twice

The program below will will open a text file, read from it to the screen, close it, open it again, prompt user to enter new text, write that back to text file and exit when the user presses the Return key.  I need it to exit when the user presses the Return key twice.  I have commented right before the line I need help with.  This program is due in 2 days - please help.


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

void text_to_screen(ifstream& file_stream);
void send_line(ofstream& file_target);

int main()
{
      ifstream file_stream;
      file_stream.open("ProgAdvice.txt");
      if (file_stream.fail())
      {
            cout << "Input file opening failed.\n";
            exit(1);
      }
      
      text_to_screen(file_stream);
      cout << "Closed file_stream \n";


      cout << "Successful \n";

      file_stream.close( );

      ofstream target_stream;
      target_stream.open("ProgAdvice.txt");
      cout << "Please add your advice to this file: \n";
      send_line(target_stream);
            target_stream.close( );

      cout << "End of this Program.\n";
      return 0;
}



      void text_to_screen(ifstream& file_stream)
      {
            char next;
            file_stream.get(next);
            while (! file_stream.eof())
            {
                  cout << next;
                  file_stream.get(next);
            }
      }

      void send_line(ofstream& target_stream)
      {
            char next;
            do
            {
                  cin.get(next);
                  target_stream << next;


//The line of code below need to test for two consecutive occurrences of the character '\n'.  I
// cannot find the correct expression for this - the code below will exit on one occurrence.
            }while (next != '\n');
                  
      }


Cad CoderDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rockiroadsCommented:
could you not store the last key pressed?
must be initialised to something

then when checking for return key,
if previous key entered is return key, then exit other store previous return key = current key

rajeev_devinCommented:
Here is the modified function to do the work.

void send_line(ofstream& target_stream)
{
      char next = 0;
      char prev;
      do
      {
            prev = next;
            cin.get(next);
            target_stream << next;
            if (next == '\n' && prev == '\n')
                  break;
            //The line of code below need to test for two consecutive occurrences of the character '\n'.  I
            // cannot find the correct expression for this - the code below will exit on one occurrence.
      }while (true);
}
rajeev_devinCommented:
>> target_stream.open("ProgAdvice.txt");
One thing that I have noticed in your code is that you are opening the file for writing.
In that case all the previous data will be removed.
I don't know whether you are doing this intentionally or accidentally :)
If you don't want the previous data to be removed then do this

ofstream target_stream;
target_stream.open("ProgAdvice.txt", ios::app); // For appending. By default it is ios::out
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Cad CoderDeveloperAuthor Commented:
rajeev devin,
Thanks for your prompt reply.  Your modified function works, however this is for a class assignment and we are not supposed to use "break" except in a case statement.  Also when I make an executable of the program it will not run properly; it just flashes a dos screen and never showes what is in the text file.  (The program is supposed to clear the text file each time you add more text, not append it.)
rockiroadsCommented:
easier way to work around is to set a boolean field

then use the boolean field in your where
i.e
int x;

x := 1;
while (x == 1)
{
    if ...
}

kinda thing
DrAskeCommented:
I like * rajeev_devin*s'  suggestion:
you can modify it to be like this:

void send_line(ofstream& target_stream)
{
     char next = 0;
     char prev;
     bool EXIT = false;
     do
     {
          prev = next;
          cin.get(next);
          target_stream << next;
          if (next == '\n && prev == '\n')
               EXIT = true;
          //The line of code below need to test for two consecutive occurrences of the character '\n'.  I
          // cannot find the correct expression for this - the code below will exit on one occurrence.
     }while (!EXIT);
}

regards;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ThummalaRaghuveerCommented:

Hi tlwaller,
                   try this...this will work without break statement

#include "stdafx.h"

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

void text_to_screen(ifstream& file_stream);
void send_line(ofstream& file_target);

int main()
{
     ifstream file_stream;
     file_stream.open("ProgAdvice.txt");
     if (file_stream.fail())
     {
          cout << "Input file opening failed.\n";
          exit(1);
     }
     
     text_to_screen(file_stream);
     cout << "Closed file_stream \n";


     cout << "Successful \n";

     file_stream.close( );

     ofstream target_stream;
     target_stream.open("ProgAdvice.txt",ios::app);
     cout << "Please add your advice to this file: \n";
     send_line(target_stream);
          target_stream.close( );

     cout << "End of this Program.\n";
     return 0;
}



     void text_to_screen(ifstream& file_stream)
     {
          char next;
          file_stream.get(next);
          while (! file_stream.eof())
          {
               cout << next;
               file_stream.get(next);
          }
     }

     void send_line(ofstream& target_stream)
     {
          char next;
              int n=0;
          do
          {
               cin.get(next);
      if((next=='\n') && n==0)
      {
             next=0;
                     n++;
      }
      else
      {
        target_stream << next;
      }
          }while (next != '\n');              
     }

Raghuveer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.