SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
I/o Streams As An Introduction To Objects And Classes
Exercise:
Programming Projects
Question:5 | ISBN:9780321531346 | Edition: 7

Question

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'...

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


const string adviceFileName = "advice.txt";

// Function to write advice to the file
void writeAdviceToFile(const string& advice) {
    ofstream outputFile(adviceFileName, ios::app); // Open the file in append mode
    if (outputFile.is_open()) {
        outputFile << advice << "\n\n"; // Write the advice to the file with two newlines separating each advice
        outputFile.close(); // Close the file
    } else {
        cerr << "Error opening the file for writing.\n";
    }
}

// Function to read the last advice from the file
string readLastAdviceFromFile() {
    ifstream inputFile(adviceFileName);
    if (inputFile.is_open()) {
        string advice;
        string currentLine;
        while (getline(inputFile, currentLine)) {
            if (!currentLine.empty()) {
                advice = currentLine; // Update the advice variable with each non-empty line
            }
        }
        inputFile.close(); // Close the file
        return advice;
    } else {
        cerr << "Error opening the file for reading.\n";
        return "";
    }
}

int main() {
    string advice;

    // Read the last advice from the file
    string lastAdvice = readLastAdviceFromFile();
    cout << "Previous advice: " << lastAdvice << endl;

    // Get new advice from the user
    cout << "Enter your advice (press Enter twice to finish):\n";
    getline(cin, advice, '\n');
    
    // Write the new advice to the file
    writeAdviceToFile(advice);

    return 0;
}

Data in "advice.txt":

Previous advice: Always use meaningful variable names and comments to improve code readability.
Enter your advice (press Enter twice to finish):
Avoid global variables as much as possible. They can lead to hidden dependencies and make code harder to understand.

 

OUTPUT OF THE PROGRAM CODE:

Previous advice: Avoid global variables as much as possible. They can lead to hidden dependencies and make code harder to understand.
Enter your advice (press Enter twice to finish):
hello marting hello

 

0 0

Discussions

Post the discussion to improve the above solution.