Hi, I'm trying to write a program which prints a file's contents for the user, then allows the user to write a paragraph which then replaces the file's contents. Right now, my problem is correctly sending the user's input to the file. I'm using getline, but for some reason it is skipping the first word of whatever the user types. Also, I'm supposed to allow an empty line between paragraphs, but it's only reading the first paragraph. Can you help? Here is my code:
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
ifstream input;
ofstream output;
input.open("ADVICE.TXT");
if (!input){
cout << "No Dice.";
exit(1);
}
string phrase1, phrase;
while (getline(input, phrase1)){
cout << "The last user wrote: " << phrase1 << endl;
}
input.close();
cout << "Please enter new code advice: " << endl;
cin >> phrase;
output.open("ADVICE.TXT");
while (getline(cin, phrase)){ //This is where the problem occurrs
output << phrase << endl; //Whenever I look in the output file, it skips the first word in phrase
cin.ignore();
exit(1);
}
output.close();
return 0;
}
You could add a EOF marker( http://www.cplusplus.com/reference/iostream/ios/eof/ ). However, a few questions have been raised about the reliability EOF.