>
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();
}