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

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!