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