I am trying to read in a text file into a string array. I am using a for loop to do that. But i have no idea how to get rid of single character words, such as: "a" "-" "." "," (Ignoring special characters and single letters). I appreciate your time, thank you so much.
#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
int main()
{
//first ask the user for a location + name from where to read the file and location + name where to save the output file,
string infile;
string outfile;
cout << "Enter the file location of file you want to read in:" << endl;
cin >> infile;
ifstream fin(infile);
if (!fin.is_open()) {
cout << "ERROR ON OPENING FILE" << endl;
exit(1);
}
else {
cout << "File successfully opened" << endl;
}
cout << "Enter the file location and name of the output file:" << endl;
cin >> outfile;
ofstream fout(outfile);
//read the contents into an array, ignoring single character words,
string wordArray[1024];
if (fin.is_open()) {
for (int i = 0; i < 1024; ++i) {
fin >> wordArray[i];
cout << wordArray[i] << endl;
}
}
cout << "Your array printed above" << endl;
cout << "------------------------" << endl;
string wordArray[1024];
if (fin.is_open()) {
for (int i = 0; i < 1024; ++i) {
fin >> wordArray[i];
if (wordArray[i].length() == 1) {
wordArray[i].erase();
}
cout << wordArray[i] << endl;
}
}
However, it is now outputting an empty line where the special character used to be.
Should i not be using the erase function? Thank you for the reply though @jonnin