Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Flags and Command Line Arguments How do i use flags, what are they, how are they

ID: 3873115 • Letter: F

Question

Flags and Command Line Arguments

How do i use flags, what are they, how are they applied here? I am really having a hard time doing this, I have no idea where to start. I am supposed to find the longest line and word(s) in each file. This is what I have until now. (The directions below must be applied to the code given.)

-m when printing the longest word in the file, print only the longest words that appear the most often

-c in addition to printing the longest words and the length of the longest line, include, in parenthesis, a count of the number of times that the word/line appears

-b consider multiple blanks between words on a line as a single blank for the purpose of calculating the length of a line

If a flag is provided that the program does not recognize, the program should print a line with the unrecognized flag followed by the string UNRECOGNIZED FLAG. The program should exit after printing this line. If a filename provided on the command line cannot be opened, the program should print a line with the filename followed by the string FILE NOT FOUND. The program should continue with the next file after printing this line.

The format of the output for each file consists of a maximum of three lines

A line with the filename followed by a colon

A line with a sorted list of the words that are the longest words in the file, comma separated

A line containing a number which is the length of the longest line in the file

#include
#include

using namespace std;

int main(int argc, char *argv[]) {
  
size_t longest = 0;
string longestWord;
int counter = 0;
string line;
string word;

if(argc == 0)
{
cout << " " << endl;
}
else
{
  
for(int i = 1;i ifstream file (argv[i]); //declare in the for loop
if (!file.is_open() ){
cout << argv[i] << " FILE NOT FOUND "; // watch out for /n
}
else{
while (getline(file,line))
{
if(line.size() > longest){
longest = line.size();
//counter = 1;
//cout << "The length of the longest Line is: " << longest << endl;
}
else if(line.size() == longest){
++counter;}
}
//cout << "Number of lines with longest length " << counter << endl;
while(file) {
file>> word;
if (word.size () > longestWord.size ())
longestWord = word;}

}
}
}
return 0;
}

Explanation / Answer

Flags are passed as command line arguments in the array argv ( 2nd parameter of your main method). 1st parameter argc represents the number of arguments to your program.

So within your code you need to iterate through the values of argv to identify the flags specified and based on the flags design the control flow for your program.

e.g.,

if(!strcmp(argv[1],"-b"){

state=2;

}

state 2 means that multiple blanks should be replaced by a single blank. Now in the main body of the code you need to check the state and if state is 2, carry out the blank replacement from the read line.

Similarly for other command line arguments as well.