-1

>

Code:

int main ()
{ 

    ifstream inStream;

    ofstream outStream; 
    getInputOutputStreams(inStream, outStream);   

    numberFile(inStream, outStream);

    return EXIT_SUCCESS;
}

I have been stuck with an issue where the code I wrote compiles, but when running it has to be terminated because it just lags. I believe my issue may have to deal with not correctly returning the names of the input and output files, but I cannot figure out where I'm going wrong. I'm only a beginner in C++ (we are just now learning about arrays), so if you think this is a dumb question I'm sorry!

Here is the code I have written:

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

using namespace std;

string getInputOutputStreams(ifstream &inStream, ofstream &outStream);

void numberFile(ifstream &inStream, ofstream &outStream);

int main ()
{
    ifstream inStream;

    ofstream outStream;
    getInputOutputStreams(inStream, outStream);

    numberFile(inStream, outStream);

    return EXIT_SUCCESS;
}

string getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
    string inputFile;
    string outputFile;

    cout << "Enter the name of the input file:" << endl;
    cin >> inputFile;
    inStream.open(inputFile);

    while (inStream.fail()) {
        cout << "Invalid file name." << endl;
        cout << "Enter the name of an input file:" << endl;
        cin >> inputFile;
        inStream.open(inputFile);
    }
    inStream.close();

    cout << "Enter the name of the output file:" << endl;
    cin >> outputFile;
    outStream.open(outputFile);

    while (outStream.fail()) {
        cout << "Invalid file name." << endl;
        cout << "Enter the name of an output file:" << endl;
        cin >> outputFile;
        outStream.open(outputFile);
    }
    outStream.close();

    return inputFile;
    return outputFile;
}

void numberFile(ifstream &inStream, ofstream &outStream)
{
    string inputFile;
    string outputFile;

    inStream.open(inputFile);
    outStream.open(outputFile);

    int lineNumber = 0;
    string line;

    while(!inStream.eof()) {
        if(line == " ") {
        }
        else {
            lineNumber++;
            outStream << lineNumber << ": " << line << endl;
        }

        getline(inStream, line);
    }

    cout << lineNumber << " lines processed" << endl;

    inStream.close();
    outStream.close();
}
  • Please format your code properly. The [editing help page](https://stackoverflow.com/editing-help#code) explains how to format code. – BessieTheCookie Mar 27 '20 at 03:20
  • Seems like your file handling itself is not clear. i suggest go through some basic file handling in c++ sample code and modify some example program to do your task as initial step. you have written mutiple returns which has no effect. So check basics of c++ aswell. – LearningC Mar 27 '20 at 03:21
  • Your next step should be a bit of [debugging](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) to determine where the hang occurs. Hardcode your input (for consistency), and add more output so you can follow the execution (or step through your program with a debugger). Focus on the problematic piece and try to come up with a [mre]. – JaMiT Mar 27 '20 at 03:43

1 Answers1

0

I see a number of mistakes:

  1. getInputOutputStreams() is closing the streams that it opens.

  2. incorrect return statements in getInputOutputStreams().

  3. numberFile() is re-opening the streams, using filename strings that have no values.

  4. using while(!inStream.eof()) is bad.

  5. not ignoring blank lines, as the instructions ask for.

Try this instead

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void getInputOutputStreams(ifstream &inStream, ofstream &outStream);

void numberFile(ifstream &inStream, ofstream &outStream);

int main ()
{
    ifstream inStream;
    ofstream outStream;

    getInputOutputStreams(inStream, outStream);
    numberFile(inStream, outStream);

    return EXIT_SUCCESS;
}

void getInputOutputStreams(ifstream &inStream, ofstream &outStream)
{
    string inputFile;
    string outputFile;

    cout << "Enter the name of the input file:" << endl;
    cin >> inputFile;
    inStream.open(inputFile);

    while (inStream.fail()) {
        cout << "Invalid file name." << endl;
        cout << "Enter the name of an input file:" << endl;
        cin >> inputFile;
        inStream.open(inputFile);
    }

    cout << "Enter the name of the output file:" << endl;
    cin >> outputFile;
    outStream.open(outputFile);

    while (outStream.fail()) {
        cout << "Invalid file name." << endl;
        cout << "Enter the name of an output file:" << endl;
        cin >> outputFile;
        outStream.open(outputFile);
    }
}

void numberFile(ifstream &inStream, ofstream &outStream)
{
    int lineNumber = 0;
    string line;

    while (getline(inStream, line)) {
        if (line != "") {
            ++lineNumber;
            outStream << lineNumber << ": " << line << endl;
        }
    }

    cout << lineNumber << " lines processed" << endl;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620