Write a program in C++ that will accept command line options to use in the progr
ID: 3844474 • Letter: W
Question
Write a program in C++ that will accept command line options to use in the program.
Command line option 1 will be the program name.
Option 2 (if used) will be ‘-p’ and that will print the keyword used in option 3.
Option 3 will be the keyword
Option 4 will be file input
Option 5 will be file output
Example:
with argv[2] (‘-p’)
./test -p KEYWORD input.txt output.txt
without argv[2]
./test KEYWORD input.txt output.txt
So, recap, argv[2] (if used) will print argv[3] (KEYWORD).
If not used, the command line will just take name of program, keyword, input, and output file names.
Explanation / Answer
Here is your program below: -
#include <iostream>
#include<string>
using namespace std;
int main ( int argc, char *argv[] )
{
if(argc == 4) {
string programName = argv[0];
string keyword = argv[1];
string inputFileName = argv[2];
string outputFileName = argv[3];
} else if(argc == 5) {
string programName = argv[0];
string keyword = argv[2];
string inputFileName = argv[3];
string outputFileName = argv[4];
cout<<"Keyword is "<<keyword;
} else {
cout<<"Wrong number of options.";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.