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?
 
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;
0
 
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

0
 
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);
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
0
 
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.)
0
 
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
0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.