Hi!
Having some major issues my homework assignment. I have to end this while loop when detecting two consecutive newline escape sequences in a row.

This is the word problem:


Write a program that gives and takes advice on program writing. The program starts by writing a piece of advice to the screen and asking the user to type in a different piece of advice. The program then ends.The next person to run the program receives the advice given by the person who last ran the program. The advice is kept in a file, and the contents of the file change after each run of the program. You can use your editor to enter the initial piece of advice in the file so that the first person who runs the program receives some advice. Allow the user to type in advice of any length so that it can be any number of lines long. The user is told to end his or her advice by pressing the return key two times. Your program can then test to see that it has reached the end of the input by checking to see when it reads two consecutive occurrences of the character ā€˜\nā€™.

Any help would be greatly appreciated.

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

ifstream reader;
ofstream writer;
void readerOpen()
{
	reader.open("hw4pr5input.txt");
	if (reader.fail())
	{
		cout<<"\nThere was an error opening the file.\n";
		exit(1);
	}
}

void writerOpen()
{
	writer.open("hw4pr5input.txt");
	if (writer.fail())
	{
		cout<<"\nThere was an error opening the file.\n";
		exit(1);
	}
}

//////////////////////////////////////////////////////////////////////////

int main( )
{
	char data;
	char tmp;
	bool escapePrevious=false;
	cout<<"\nSolution to Homework Assignment 4 Problem 5\n";

	readerOpen();
	
	cout<<"\nAdvice of previous user: "<<endl;
	while(!reader.eof())
	{
		reader.get(data);
		cout.put(data);
	}

	reader.close();
	writerOpen();
	data = ' ';
	cout<<"\nPlease enter your advice. End by hitting Enter twice:\n\n";	

//////////////////////////////////////////////////////////////////////////////

	while(!writer.eof() && escapePrevious != true && tmp != '\n')
	{
		cin.get(data);
		writer.put(data);
		if (data == '\n')

			{
				if(escapePrevious==true)
					{
						tmp = '\n';
					}
				else 
					{
						escapePrevious=true;
					}
			}
	}
	/* the way i see it, if i press the return key, the conditional with the if(data == '\n') will skip the first conditional statement and assign escapePrevious true, and then if the condition is tested a second time and escapePrevious is already true, tmp will be assigned the newline character, thus negating the condition in my while loop. Im doing something terribly wrong here because it is exiting the loop the first time i press return*/ 

	writer.close();
	
	cout<<"\nThanks for the advice."<<endl;
	
    system("Pause");
    return 0;
}

Recommended Answers

All 2 Replies

Member Avatar for iamthwee

Forget the writing to the file and everything else.

Concentrate on just getting the command prompt to recognise two newlines next to one another and then terminate the program.

figured it out. Thanks!

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

ifstream reader;
ofstream writer;
void readerOpen()
{
	reader.open("hw4pr5input.txt");
	if (reader.fail())
	{
		cout<<"\nThere was an error opening the file.\n";
		exit(1);
	}
}

void writerOpen()
{
	writer.open("hw4pr5input.txt");
	if (writer.fail())
	{
		cout<<"\nThere was an error opening the file.\n";
		exit(1);
	}
}

int main( )
{
	char data;
	char tmp;
	bool escapePrevious=false;
	cout<<"\nSolution to Homework Assignment 4 Problem 5\n";

	readerOpen();
	
	cout<<"\nAdvice of previous user: "<<endl;
	while(!reader.eof())
	{
		reader.get(data);
		cout.put(data);
	}

	reader.close();
	writerOpen();
	
	data = ' ';
	cout<<"\nPlease enter your advice. End by hitting Enter twice:\n\n";	

	cin.get(data);
	while(!writer.eof() && !(data == '\n' && tmp == '\n'))
	{
		writer<<data;
		tmp = data;
		cin.get(data);
	}
	 
	writer.close();
	
	cout<<"\nThanks for the advice."<<endl;
	
    system("Pause");
    return 0;
}
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.