We help IT Professionals succeed at work.

Exit C++ program by pressing the enter key twice

Cad Coder
Cad Coder asked
on
Medium Priority
540 Views
Last Modified: 2006-11-18
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');
                  
      }


Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
>> 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
Cad CoderDeveloper

Author

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.)
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
Commented:
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;

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.