Take the contents of an input text file and write them in reverse order of words to an output file. Your program can ignore line breaks. You will need to use arrays.
(unnecessary crap removed) Aaaaah, panic, please help!
EDIT: What I have so far. Still lost on how to reverse word order now.
//Kristen Korz
//CIS 22A
//This program reads an input file and writes the words in reverse order to an output file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
//create and link input...
ifstream inputFile;
inputFile.open("input.txt");
//...and output files
ofstream outputFile;
outputFile.open("output.txt");
//error message for file open fail
if (inputFile.fail())
cout << "Error opening the file.\n";
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str;
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << endl;
system("pause");
return 0;
}
What I've got successfully reads the input file word for word. I can figure out everything except how to reverse the word order for then writing to output.txt This is our first program reversing the order of things, yes.
EDIT 2:
Okay, so the best I can guess is this:
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str; //note: variables will be used for output loops too
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << endl;
//for writing in reverse word order to output file
for (int i = MAXSIZE-1; (outputFile << str) && (i >= 0); --i)
{
words[i] = str;
}
outputFile.close();
//for showing if written correctly
for (int i= MAXSIZE-1; i >= 0; --i)
{
cout << words[i] << endl;
}
The input section works fine. Output just repeats last word of input for each iteration.
EDIT 3:
Just kidding, everything except actual writing of output file works. Output in the terminal is corrects by getting rid of the "-1" after MAXSIZE
in the initialization. Adjusting the code that writes the file in a similar way does not solve the repeating "works." (final word of input file) written to output.txt
EDIT 4:
Relevant code is now:
//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str; //note: variables will be used for output loops too
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();
//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << " ";
//for writing in reverse word order to output file
for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
{
words[i] = str;
}
outputFile.close();
//for showing if written correctly
for (int i = MAXSIZE; i >= 0; --i)
{
cout << words[i] << " ";
}
If I change i>=0
to i=0
(which I mistakenly typed first try) in for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
, then cout
in terminal is perfect. Can't figure out how to get output file to not be repeated "works." Why is it doing that? Note: output to terminal optional, so I don't really care why that wants i
assigned to 0 in previous for loop in terms of being able to finish the assignment