Get up to 80 % extra points for free! More info:

ifstream and ofstream errors

Solution for the forum question https://www.ict.social/cplusplus/cplusplus-forum/can-anyone-help-me-understand-what-my-errors-mean--5a1f9beb1b394 It's functional, but probably not working according to author expectations.

cpp

/*  Program:        prog5B.cpp
    By:             Mackenzie Ritter
    Last Modified:  Nov 23, 2017
    Purpose:        To produce a filled out madlibs with users help.
    Notes:
*/
#include <fstream>
#include <iostream>

using namespace std;

int openInfile(ifstream &);

int openOutfile(ofstream &);

void change(string &, string &);

int main() {
    string line = " ";
    string word = " ";
    string diffLine = " ";
    ifstream ins;  // ins is an input stream
    ofstream outs; // outs is an output stream
    openInfile(ins);
    change(word, diffLine);
    while (getline(ins, line)) {
        outs << line << endl;
    }
    ins.close();
    outs.close();
}

/*  Function:   openInfile
    Last Modified:  Nov 23, 2017
    Purpose:    Opens the input file after getting file name from user.
    In Parameters:  None
    Out Parameters: string fileName
    Return Value:   None
*/
int openInfile(ifstream &ins) {
    string fileName = " ";
    cout << "Enter file of madlibs outline." << endl;
    cin >> fileName;
    ins.open(fileName.c_str()); // connects ins to file inFile

    if (ins.fail()) {
        cerr << "Error: Unable to open file : FILENAME" << endl;
        return -1; //return if failure
    } else {
        ofstream outs; // outs is an output stream
        return openOutfile(outs);
    }
}

/*  Function:       openOutfile
    Last Modified:  Nov 23, 2017
    Purpose:        Opens the output file after getting file name from user.
    In Parameters:  None
    Out Parameters: string copyFile
    Return Value:   None
*/
int openOutfile(ofstream &outs) {
    string copyFile = " ";
    cout << "Enter name of file for updated data." << endl;
    cin >> copyFile;
    outs.open(copyFile.c_str());

    if (outs.fail()) {
        cerr << "Error: Unable to open file : FILENAME" << endl;
        return -1; // return if failure
    } else {
        return 0;
    }
}

/*  Function:       change
    Last Modified:  Nov 23, 2017
    Purpose:        Replaces blanks with words from user.
    In Parameters:  string word, diffLine
    Out Parameters: string fileName
    Return Value:   None
*/

void change(string &word, string &diffLine) {
    string searchN = "blank-N";
    string searchA = "blank-A";
    string searchV = "blank-V";
    string searchP = "blank-P";
    string searchD = "blank-D";
    string noun, adjective, verb, place, adverb;
    if (word == searchN) {
        cout << "Enter a noun." << endl;
        cin >> noun;
        noun = searchN;
    } else if (word == searchA) {
        cout << "Enter an adjective." << endl;
        cin >> adjective;
        adjective = searchA;
    } else if (word == searchV) {
        cout << "Enter a verb." << endl;
        cin >> verb;
        verb = searchV;
    } else if (word == searchP) {
        cout << "Enter a place." << endl;
        cin >> place;
        place = searchP;
    } else if (word == searchD) {
        cout << "Enter an adverb." << endl;
        cin >> adverb;
        adverb = searchD;
    } else {
        word = word;
    }
} 

Raw

Added: 12/1/2017
Expires: Undefined

Avatar
Author: Inactive member
Activities