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

Can anyone help? Thanks? C++ Programming Oracle Write a program that gives and t

ID: 3909371 • Letter: C

Question

Can anyone help? Thanks? C++ Programming Oracle
Write a program that gives and takes advice on program writing. The program writes a piece of advice to the screen, then asks for advice from the user. The next person to run the program receives the advice from the last user. Between runs, advice is kept in a file, which is overwritten each time the program is run. The programmer creates the initial advice file (using a text editor.) Since the advice is to be of unlimited length, the programmer signals the user to terminate by pressing <cr>twice.
Structure: Tell the user the program function read the advice file, output to screen (line by line) request input from the user accept input from user, write to file (line by line)
Discussion: First, have the student type in a file with at text editor to have data. The student should then work on getting the file read and written to the screen. Once this has been done, the input from the file of the new advice can be tackled. Use the same external file for input and output. It will be necessary to close then reopen this file using a different fstream variable. For the loop that accepts the new advice, we chose to extract two characters, test for two consecutive<cr> , use a flag to stop the input loop, then use the putback member function to restore the input stream.
Can anyone help? Thanks? C++ Programming Oracle
Write a program that gives and takes advice on program writing. The program writes a piece of advice to the screen, then asks for advice from the user. The next person to run the program receives the advice from the last user. Between runs, advice is kept in a file, which is overwritten each time the program is run. The programmer creates the initial advice file (using a text editor.) Since the advice is to be of unlimited length, the programmer signals the user to terminate by pressing <cr>twice.
Structure: Tell the user the program function read the advice file, output to screen (line by line) request input from the user accept input from user, write to file (line by line)
Discussion: First, have the student type in a file with at text editor to have data. The student should then work on getting the file read and written to the screen. Once this has been done, the input from the file of the new advice can be tackled. Use the same external file for input and output. It will be necessary to close then reopen this file using a different fstream variable. For the loop that accepts the new advice, we chose to extract two characters, test for two consecutive<cr> , use a flag to stop the input loop, then use the putback member function to restore the input stream.
C++ Programming Oracle
Write a program that gives and takes advice on program writing. The program writes a piece of advice to the screen, then asks for advice from the user. The next person to run the program receives the advice from the last user. Between runs, advice is kept in a file, which is overwritten each time the program is run. The programmer creates the initial advice file (using a text editor.) Since the advice is to be of unlimited length, the programmer signals the user to terminate by pressing <cr>twice.
Structure: Tell the user the program function read the advice file, output to screen (line by line) request input from the user accept input from user, write to file (line by line)
Discussion: First, have the student type in a file with at text editor to have data. The student should then work on getting the file read and written to the screen. Once this has been done, the input from the file of the new advice can be tackled. Use the same external file for input and output. It will be necessary to close then reopen this file using a different fstream variable. For the loop that accepts the new advice, we chose to extract two characters, test for two consecutive<cr> , use a flag to stop the input loop, then use the putback member function to restore the input stream.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main(){

   ifstream fin("log.txt");
   if (!fin){
      cout << "Error opening file ";
      return 0;
   }
   string line;
   cout << "Previous advice: ";
   while(getline(fin,line)){
      cout << line;
   }
   fin.close();
   ofstream fout("log.txt");
   cout << " Enter your advice: ";
   while(true){
      char a = cin.get();
      char b = cin.get();
      if (a == ' ' && b == ' ')
         break;
      else
        fout <<a<<b;
   }
   fout.close();
   return 0;
}