• C

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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
komlaaaAuthor Commented:
Thanks, that did it!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.