using command line flags in order

This is basically how my main looks like.see below. also see the sample run for what the different flags do.
I would like to be able to print my output in order of the flags. How can i do that? or do i have make a lot of changes:

======================= MAIN ================================
int main( int argc, char * const argv[])
{
  wc w;

  bool checkL = false;
  bool checkT = false;
  bool checkD = false;
  bool checkC = false;
  bool noArgument = false;

  int ch;

  while ((ch = getopt(argc, argv,":ltdc")) != -1) {
    switch (ch) {
    case 'l':
      checkL = true;
      break;
    case 't':
      checkT = true;
      break;
    case 'd':
  checkD = true;
      break;
    case 'c':
      checkC = true;
      break;
    case '?':
    default:
      std::cout<<"Invalid option specified: "<< optopt<<"\n";
      w.helpMenu();
      exit(1);
      break;
    }

  }

  if(optind == 1){
    noArgument = true;
  }
 w.mostCommonWord();

  if( true == checkT ){
    std::cout<<"TOTAL: "<<w.getTotalCount()<<endl;
  }

  if( true == checkD ){
    std::cout<<"DISTINCT: " <<w.getDistinctCount()<<endl;
  }

 if( true == checkC ){
   std::cout<<"MOST COMMON WORD: "<<w.getMostCommonWord()<<" "<<w.getMostCommonCount()<<endl;
 }

 if( true == checkL ){
  w.display(w.begin(), w.end());
 }

 if( true ==  noArgument ){ //no other argument is provided.
 w.display(w.begin(), w.end());
 std::cout<<"TOTAL: "<<w.getTotalCount()<<endl;
 std::cout<<"DISTINCT: " <<w.getDistinctCount()<<endl;
 std::cout<<"MOST COMMON WORD: "<<w.getMostCommonWord()<<" "<<w.getMostCommonCount()<<endl;

 }
==================== SAMPLE RUN ============================
crux:~/achille/a4_1> uname -rs
SunOS 5.8
crux:~/achille/a4_1> ls
#f.out#      BUG_LOG      Makefile_1   bible.txt    f.out        file~        main.cpp     main1.cpp    wc.cpp
#test.out#   MANPAGE      README       cs450words*  file         getopt.c     main.cppn    main_1.cpp   wc.h
#wc.h#       Makefile     TIME_LOG     error        file1        getopt.o     main.o       test.out     wc.o
crux:~/achille/a4_1> cat file
The grace of our Lord Jesus Christ be with you all. Amen.
crux:~/achille/a4_1> make
make: Nothing to be done for `all'.
crux:~/achille/a4_1> cs450words -l file
Amen 1
Christ 1
Jesus 1
Lord 1
The 1
all 1
be 1
grace 1
of 1
our 1
with 1
you 1
crux:~/achille/a4_1> cs450words -d file
DISTINCT: 12
crux:~/achille/a4_1> cs450words -t file
TOTAL: 12
crux:~/achille/a4_1> cs450words -c file
MOST COMMON WORD: Amen 1
crux:~/achille/a4_1> cs450words -l -t -c file
TOTAL: 12
MOST COMMON WORD: Amen 1
Amen 1
Christ 1
Jesus 1
Lord 1
The 1
all 1
be 1
grace 1
of 1
our 1
with 1
you 1
crux:~/achille/a4_1>
komlaaaAsked:
Who is Participating?
 
van_dyConnect With a Mentor Commented:
this is fairly simple to do if you use a character array.
like say in the beginning fo your main you do something like:

  bool checkD = false;
  bool checkC = false;
  bool noArgument = false;
  char options[10];                     // we remember the options in order in ths array
  int ch;
   int i = -1;
   int j = 0;

while ((ch = getopt(argc, argv,":ltdc")) != -1) {
    ++i;
    switch (ch) {
    case 'l':
      checkL = true;
      break;
      options[i] = 'l';           // add same lines for all the switch cases
    case 't':
      checkT = true;
      options[i] = 't';        // similarily for d and c
      break;
    case 'd':
  checkD = true;
      break;
    case 'c':
      checkC = true;
      break;
    case '?':
    default:
      std::cout<<"Invalid option specified: "<< optopt<<"\n";
      w.helpMenu();
      exit(1);
      break;
    }


now when you have to print out the results use a while loop
while(j <= i){
    switch(options[j]){
     case 'l':
               w.display(w.begin(), w.end());
               break;
      case 't':
                  std::cout<<"TOTAL: "<<w.getTotalCount()<<endl;
                  break;
 ..........//similarily for other cases, i hope you get the general idea
           
0
 
komlaaaAuthor Commented:
Thanks, that did it!!!!
0
All Courses

From novice to tech pro — start learning today.