We've just started I/O streams and i need some help with my prjct.

Here is the lab: "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 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 is or her advice by pressing the Return key two times. Your program can then test to see that it has reaches the end of the input by checking to see when it reads two consecutive occurrences of the character’\n’."

My code so far

[#include<iostream>
#include<fstream>
using namespace std;
int main(){
    
char symbol;
ifstream input;    
ofstream output;

input.open("input2.txt");



while(!(input.eof())){
input.get(symbol);
cout<<symbol;
}

cout<<endl;
cout<<endl;
cout<<"Enter your advice.\n"
<<"Press enter twice when finished.\n";

do{
cin.get(symbol);
}while(symbol!='\n');

input.close(); 
system("pause");
}
]

My problem is how to get the input sent to the file, and check if return is hit twice.

Edited 6 Years Ago by WaltP: Added CODE Tags -- where do you people get the ides that you can put code between [] and it works?

to check to see if the user presses enter. You will need to create an integer value to hold how many times it was pressed and set it to zero. If enter is pressed add one to the integer, else set it to zero. If the integer equals 2 move on to the next step.

And here is a great tutorial to explain how to edit files http://www.cplusplus.com/doc/tutorial/files/
I hope this helps.

Edited 6 Years Ago by noey699: n/a

As I understand it, the instructions mean the advice you type can't have blank lines, because if a blank line is entered (two enters in a row creates a blank line), then input should stop. Maybe something like this would work:

1) declare three STL string objects---fileData, firstLine and nextLine
2) read in fileData from file and display to screen
3) then get first line of new input in firstLine
4) in the body of an infinite loop get the next line of input in nextLine. If nextLine is empty, then break out of the loop, stopping further input. Otherwise, concatenate nextLine onto firstLine and keep going.
5) when the loop is done, either write fileData followed by a newline char and finally firstLine to file using an output file stream in truncate mode (which is the default mode for output streams) or write a newline char followed by firstLine to file using append mode (ios::app).

N.B. I suppose if you call a line of spaces a blank line then the appearance to the output would like a blank line, too, so I guess it depends on how you define what a blank line is whether the advice you type could have blank lines or not. The above pseudocode could be used to add lines containing just spaces if desired.

Edited 6 Years Ago by Lerner: n/a

Simple. Read an entire line rather than a single character.
Output the line to the file.
If the first character in the line is \n, you're done. Nothing was entered.

This article has been dead for over six months. Start a new discussion instead.