Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that gives and takes advice on program writing. The program

ID: 3660877 • Letter: W

Question

Write a C++ 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 Enter 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 ' '. Please use at least 4 functions besides the main function: 1) a function to initialize the advice.txt file for reading; 2) a function to

Explanation / Answer

The required code is:--
#include
using namespace std;
int main()
{

string adviceFile = "ädvice.txt";
string advice = read_advice_from_console(adviceFile);
if(advice ="") {
write_Advice_To_File("This is initial advice",adviceFile);
}

write_advice_to_screen(advice);
cout << "Write your advice";
advice = read_advice_from_console();
write_Advice_To_File(advice,adviceFile);
}

void write_advice_to_screen (string advice) {
cout << advice;
}

string read_advice_from_console() {
string advice;
string line = "";
int count =0;
while (count !=2) {
getline (cin, line);
advice = advice+line;
if(line == " ") {
getline (cin, line);
if(line == " ")
count = 2;
else
advice = advice+line;
}

}
return advice;
}

string read_Advice_From_File(string filename) {

string advice = "";
string line;
ifstream myfile (filename);
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
advice = advice+" "+line;
}
myfile.close();
}
return advice;
}

void write_Advice_To_File(string advice, string filename) {
ofstream myfile ("example.txt",ios::out | ios::trunc );
if (myfile.is_open())
{
myfile << advice;
myfile.close();
}
}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote