0

My task is to add a certain adjective if the words "the", "an", or "a" are found from an input file. Instead of having a large number of if statements with a bunch of or's, I was wondering if there was a more elegant way of writing this code.

An idea I had was to store "the", "an", and "a" in an array. What I wanted to then do was check the input file if any of those articles are present.

The if statements I used doesn't accomplish this. Sorry I am new.

   //Run in Command Line
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>


using namespace std;
int main (int argc,char *argv[])
{
string fileName;
ifstream inputFile;
string adjective;

cout << argc <<endl; //sees whether the correct number of arguments are being passed through
cout << argv[0] <<endl; //checks that
cout << argv[1] <<endl;   //the correct arguments
cout << argv[2] <<endl;     //are being passed through
if (argc == 3)
{
    adjective = argv[1];
    inputFile.open(argv[2], ios::in);
}
else
{
    cout<< "File not found." <<endl;
    exit(1);
}
//------------------------------------------------------------//


char firstLetter = adjective[0];
string index = " ";
string ArticleThe[] = {"THE", "THe", "The",
                       "tHE", "thE", "tHe",
                       "ThE", "the"};
string ArticleA[] = {"A", "a"};
string articleAn[] = {"AN", "An", "aN", "an"};


while (!inputFile.eof()) //while not at the end of file
{
    inputFile >> index; //traverse through string

    {
        if (articleThe.find(index) != articleThe.end())
        {
            inputFile >> index; //if article 'the' is found, move to noun
            if (!inputFile.eof())
            {
                //display memo with adjective added after all "the" articles
            }
        }

        else if (articleA.find(index) != articleA.end() && adjective.isVowel(firsLetter) == 1)
        {
            inputFile >> index;
             if (!inputFile.eof())
            {
                //display memo with adjective added after all "a" articles
            }
        }
        else if (articleAn.find(index) != articleAn.end() && adjective.isVowel(firsLetter) != 1)
        {
            inputFile >> index;
             if (!inputFile.eof())
            {
                //display memo with adjective added after all "an" articles
            }
        }
    }





}



}

bool isVowel(char firstLetter)
{
    if (firstLetter == 'a'
    || firstLetter == 'e'
    || firstLetter == 'i'
    || firstLetter == 'o'
    || firstLetter == 'u'
    || firstLetter == 'A'
    || firstLetter == 'E'
    || firstLetter == 'I'
    || firstLetter == 'o'
    || firstLetter == 'u')
    return true;

else
    return false;
}
  • possible duplicate of [C++ searching a line from a file for certain words and then inserting a word after those words](http://stackoverflow.com/questions/28507718/c-searching-a-line-from-a-file-for-certain-words-and-then-inserting-a-word-aft) – Christophe Feb 13 '15 at 23:14

1 Answers1

0

You can simply use a regex instead of writing a state machine yourself:

regex re("(^|[^_[:alnum:]])(the|a)($|[^_[:alnum:]])", regex_constants::icase);

if(regex_search("the test", re))
    cout << "#1 matches!\n";

if(regex_search("A TEST", re))
    cout << "#2 matches!\n";

if(regex_search("wat?", re))
    cout << "#3 matches!\n";

Shows that #1 and #2 match.

danielschemmel
  • 10,885
  • 1
  • 36
  • 58