Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using command line flags in order

Posted on 2004-10-30
2
Medium Priority
?
344 Views
Last Modified: 2010-04-15
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>
0
Comment
Question by:komlaaa
2 Comments
 
LVL 5

Accepted Solution

by:
van_dy earned 1200 total points
ID: 12450827
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
 

Author Comment

by:komlaaa
ID: 12454490
Thanks, that did it!!!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question