Thread: looping a switch statement with argc as cases

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    looping a switch statement with argc as cases

    Is it possible to build a loop around a switch statement using argc as cases b/c I want to have differing command line args.
    e.g. [convert_dec_to_bin] [target] [print] or [convert_dec_to_bin] [target] [check_status] [shift]

    so the above are the command lines args in argv: argv[0], argv[1] etc. b/c I want to build a simple base-10 to base-2 conversion program and as long as user doesn't want to quit, they can re-enter new command line args. I know it works w/ using prompt, but if I didn't want that (such as cin, getline), is it possible? Somehow I think not b/c we need to re execute the exe(convert_dec_to_bin.exe)...not sure

    e.g.
    Code:
    while ( input != 'q' )
    {
       switch (argc )
          case 2: 
                //do this
          case 3:
              //do this
    }
    Last edited by monkey_c_monkey; 08-06-2012 at 07:51 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it is possible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    87
    I'm not sure exactly how. This is what I have:
    So the functions I wrote are:
    1)print_bits_ : converts base-10 to binary
    2) is_on : to test if a single bit in target val is on or not
    3) turn_on : turn on the bit
    4) turn_off : " " off " "

    Code:
    void dec_to_bin(int argc, char** argv)
    {
        string menu
        (
            "Radix-10 to Binary converter v1.0 by monkey_c_monkey\n"
            "=========================================\n"
            "How to use: (NB: all inputs are optional)\n"
                "low [target] [check status] [change bit in target] [print target in binary]\n"
            "[TARGET]\n"
                "e.g. 2 (any unsigned integer) or '_' if you want to use previous target\n"
            "[CHECK STATUS]\n"
                "[target] [s] [which bit to shift to for the mask]\n"
            "[CHANGE BIT IN TARGET]\n"
                "[1] turn on bit: [target] [o] [which bit]\n"
                "[2] turn off bit: [target] [f] [which bit]\n"
            "[PRINT]\n"
                "[target] [p]\n"
            "[HELP]\n"
                "prints instructions to use program: [h]\n"
        );
        
        //the inputs to program
        unsigned int target;
        char option = ' ';
        unsigned int shift = 0;    
        unsigned int your_target = 0;//in case user forgets to enter it
    
        target = atoi(argv[1]);
        option = *argv[2];
        shift = atoi(argv[3]);
                
        if ( target != '_' )//we want to continue using the initial target input user entered
                your_target = target;
        else 
            your_target = your_target;//keep as is
        
        switch (argc)
        {    
            case 3://[exe] [t]/[_] [p]/[h]
                if ( option == 'h' )
                    cout << menu << endl;
                else if ( option == 'p' )
                    print_bits_(your_target);
                else if ( option == 's' )
                    is_on(your_target, shift);
                else if ( option == 'o' )
                    turn_on(your_target, shift);
                else if ( option == 'f' )
                    turn_off(your_target, shift);
                break;
            case 4://[exe] [t]/[_] [s]/[o]/[f] [shift]
                if ( option == 's' )
                    is_on(your_target, shift);
                else if ( option == 'o' )
                    turn_on(your_target, shift);
                else if ( option == 'f' )
                    turn_off(your_target, shift);
                break;
        }
    }
    
    int main(int argc, char* argv[])
    {
       char input = 'n';
       string continue = "Do you want to run program again (y\n)?";
       if ( argc > 1 )
       {
         do
         {
            dec_to_bin(argc, argv);
            cout << continue << endl;      
         }
          while ( input == 'y' );
       }
      return 0;
    }
    EDIT: it doesn't crash anymore since I just forgot to check condition to make sure:
    Code:
    if ( argc > 1 )
    and all functions work fine, but it exits, so how can I get chance to choose input of 'y' to continue?

    So when I run first time in cmd.exe (I'm using Windows...):
    ============================================
    C:\Users\joey\Desktop\cpp_workspace>conver_dec_to_ bin 2 p
    0000 0000 0000 0000 0000 0000 0000 0010
    Do you want to run program again (y/n)?

    C:\Users\joey\Desktop\cpp_workspace>

    *program exits, so I don't have chance to answer the prompt: "Do you want to run program etc....?"
    Last edited by monkey_c_monkey; 08-06-2012 at 08:17 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I have to say that you're on the right track but a switch case for argc needs to have a default case.

    I might decide to do something like
    low 123456 o 4 p h

    And you will have to decide what to do about it.

    edit: I also don't think looping is a good idea. You have to rerun the program to get new arguments.
    Last edited by whiteflags; 08-06-2012 at 08:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement does not flow through cases
    By sjmp in forum C Programming
    Replies: 17
    Last Post: 07-27-2012, 07:12 AM
  2. Need help looping this switch statement
    By nick753 in forum C++ Programming
    Replies: 12
    Last Post: 01-18-2010, 03:36 AM
  3. Need help looping this switch statement
    By ravens199 in forum C++ Programming
    Replies: 29
    Last Post: 01-16-2008, 03:06 PM
  4. switch cases and do while
    By exoillusion in forum C Programming
    Replies: 7
    Last Post: 07-28-2003, 07:18 AM
  5. could someone please explain switch cases?
    By .exe in forum C++ Programming
    Replies: 7
    Last Post: 07-01-2003, 01:42 PM