0

I tried to look for my problem all over stackoverflow, and found nothing similar. I am working an an assignment that requires me to go through each argument, and if it is a text file, to output the textfile's length for each argument given.

The main part of the function I have no trouble with. The only problem I have is that we have to have certain flag (denoted as '-c'), which if the flag was in the argument, it would change the behavior of the main program. For instance, '-c' would just output the contents of the textfiles instead of printing out it's length.

I understand that they way to go by this using boolean values, seeing if the flag is in the argument or not. However, no matter what method I try, my compiler keeps coming up with this mysterious expected unqualified id.

My code begins with

int main(int argc, char *argv[]){
    for ( i = 1; 1 < argc; i++)        // iterating through each textfiles
}

I want the program to see if argv[i] is the flag that I defined, but whatever method I try to implement the flag, I always get this error.

bool isflag (string -c)

or

bool -c;
-c = true; 
if (isflag){
...
}

And none of these work. I assume it has something to do with the dash character. I'm just really in a hunch and I have no idea what to do to solve this.

user78154
  • 1
  • 1
  • 1
    Variable names cannot contain dashes. Your C++ variable name doesn't have to match the command line argument value you're looking for. You will be doing some kind of string comparison to see if it's contained in the argv[] array. – Jim Lewis Oct 06 '17 at 21:56
  • Interesting. So what you're saying is make the bool variable completely different and then have a string comparison if the '-c' is in the array? But then how would I do the string comparison if I cannot use dashes in the first place... – user78154 Oct 07 '17 at 01:51
  • As well as the excellent answer below, you might want to read [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as well as your tutorial materials. – Martin Bonner supports Monica Oct 09 '17 at 07:03

1 Answers1

0

You seem to be having a bit of confusion between the name of a variable, and the value stored in that variable. Here's a fragment of C++ code showing how you can check strings against command line arguments, and set boolean flags if they match. It assumes "-c" will be the first command line argument, if it's present at all.

int main(int argc, char *argv[])
{

   std::string argument = "-c";      /* the string you're looking for */
   bool flag = false;                /* Was the string found? */
   int filename_start_position = 1;  /* Where do the filenames start in the argument list? */

   /* check first argument to see if it's the -c option */

   if (argument == argv[1])  /* note, using == for string comparison only works if at least one of the arguments is a std::string */
   {
     flag = true;    /* -c option found, set the flag */
     filename_start_position = 2;  /*no need to process argv[1] further */
   }

   /* Process remaining arguments */

   for(int i = filename_start_position; i < argc; i++)
   {
      if (flag)
      {
          /* print contents of file argv[i] */
      }
      else
      {
          /* calculate length of file argv[i] */
      }
   }    
}
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • 1
    For real code, you would probably want to call the variable `cflag` because there will probably be more than one such flag. I'd also want to give `argument` a better name, and make it `static const` - but these are all mere details compared to the huge conceptual leap the OP needs to make. – Martin Bonner supports Monica Oct 09 '17 at 07:07
  • @Martin You're right, of course...I wanted to keep the example as simple and focused as possible to illustrate the point OP seems to be struggling with. – Jim Lewis Oct 09 '17 at 07:16
  • Yup. Totally agree with that. I just wanted to make the point (to the OP and other readers rather than yourself) that this is code designed to illustrate a single point rather than the pinnacle of good C++ style for production code. – Martin Bonner supports Monica Oct 09 '17 at 07:21