Give a main function written in C++. Your main function is to see if it has been passed exactly one command line argument (you are to check that there is exactly one), and it has the value
–verbose
The function will set the local integer variable foundVerbose to 1 if there is exactly one argument and that argument is -verbose, otherwise foundVerbose is set to 0. The variable should be local to the function.

I am reviewing for an exam and came across this question and don't have a clue how to start.

Recommended Answers

All 3 Replies

See the C function getopt(). Alternatively, iterate over the argv[] array passed on the command line to main(int argc, const char* argv[]). If you don't know about this stuff, you have some studying to do before you try to solve this assignment.

Oh thanks for refreshing my memory:

int main(int argc, char** argv) {
    if (argc == 1 && argv[0]= -verbose){
        foundVerbose = 1;}
    else{
        foundVerbose = 0;}}

The first arg will either be the program name or empty, it's the second arg that will contain "-verbose", so you'll need to check that argc == 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.