Feb 14, 2019 at 2:40am UTC
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
typedef std::vector<std::vector<char> > LineWords;
using namespace std;
int main()
{
//take the input file name
char substitution[80], words[80];
cout << "\nEnter substitution file name: \n";
cin.getline(substitution , 80);
cout << "\nEnter word file name: \n";
cin.getline(words , 80);
//open the input file to read
ifstream substitutionFileout, wordFileOut;
substitutionFileout.open(substitution);
wordFileOut.open(words);
//checking if the input files (substitution and word) are valid
if(!substitutionFileout.is_open()|| !wordFileOut.is_open())
{
cout << "BAD FILE FILENAME" << endl;
exit(0);
}
//Reading substitution file
string line;
std::vector<std::vector<char> > lineWords;
std::vector<std::vector<std::vector<char> >> lines;
map<vector<char>, vector<char>> subt;
while(std::getline(substitutionFileout, line))
{
string::iterator it;
bool newString = true;
lineWords.clear();
for ( it = line.begin() ; it < line.end(); it++)
{
if(isspace(*it) || ispunct(*it))
{
newString = true;
continue;
}
if(newString)
{
lineWords.push_back(std::vector<char>());
newString = false;
}
lineWords[lineWords.size() - 1].push_back((*it));
}
if(lineWords.size() != 2)
{
lineWords.clear();
}
if(lineWords.size())
{
lines.push_back(lineWords);
subt.insert(lineWords[0], lineWords[1]);
}
}
if(!lines.size())
{
cout << "Empty Substitution file. Please check" << endl;
exit(0);
}
//Reading word file and replacing
//map<vector<char>, vector<char>>::iterator it1;
std::vector<char> word;
while(std::getline(wordFileOut, line))
{
string::iterator it;
bool newString = true;
for ( it = line.begin() ; it < line.end(); it++)
{
if(isspace(*it) || ispunct(*it))
{
if(subt.find(word) == subt.end())
{
for(unsigned int i = 0; i < word.size();)
{
cout << word[i];
i+=1;
}
}
else{
vector<char> newWord = subt[word];
for(unsigned int i = 0; i < newWord.size();)
{
cout << newWord[i];
i+=1;
}
}
newString = true;
cout << *it ;
continue;
}
if((*it) == '\0')
{
if(subt.find(word) == subt.end())
{
for(unsigned int i = 0; i < word.size();)
{
cout << word[i];
i+=1;
}
}
else{
vector<char> newWord = subt[word];
for(unsigned int i = 0; i < newWord.size();)
{
cout << newWord[i];
i+=1;
}
}
newString = true;
cout << endl;
}
if(newString)
{
word.clear();
newString = false;
}
word.push_back((*it));
}
}
return 0;
}
Feb 14, 2019 at 5:01am UTC
Your code is very hard to read since you didn't bother using code tags.
And you didn't indicate which line the error refers to. I can't see it.
And you didn't bother to say what the program does, either.
Feb 14, 2019 at 6:00am UTC
Why all the C-strings, you really should be using C++ strings instead of the C-strings and vector of char.