• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

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');
                  
      }


0
Cad Coder
Asked:
Cad Coder
2 Solutions
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now