Solved

using command line flags in order

Posted on 2004-10-30
295 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
Question by:komlaaa
    2 Comments
     
    LVL 5

    Accepted Solution

    by:
    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
    Thanks, that did it!!!!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    How do I test for current date? 9 77
    memory leak detection 9 30
    how do i resovle this problem in a dll? 1 318
    negation in C function 14 119
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now