Getopt Function

This is a discussion on Getopt Function within the C++ Programming forums, part of the General Programming Boards category; Does anyone have a good example/tutorial of how to handle flags using getopts or another structure with values to be ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Argument handling

    Does anyone have a good example/tutorial of how to handle flags using getopts or another structure with values to be read in right after the flags. I'm using a generic switch checking for the '-' character and its not working as I plan because, I need to be able to move the flags around and still have it read in the correct input. If anyone has a small/simplistic example or tutorial that would be very helpful. Thank you.
    Last edited by cisokay; 05-15-2005 at 02:51 PM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    53

    Argument handling

    I have a program that can take in 3 arguments. They can be called in almost any order and they can be invoked more than once. I'm trying to use a switch statment to determin which case is called, but the problem is each flag also takes in a parameter and the main param that the program calls is shifted everytime a flag is called. I'm really not sure how to handle this.

    Sample Run:

    test -c 35 //counts the number of 1's in base 2 for 35
    test -c -s 3 35 //sets the 3 value of the base 2 number of 35 and then count up the number of 1's // base 2 of 35 = 100011 so if you set the 3rd bit it would be coming from the Right hand side so the value will then be 100111
    test -c -s 3 -z 2 35 //sets the 3rd bit to 1 and then 2nd bit to 0 of the value 35

    and so fourth. . . . I have functions to handle these sets, but I'm unsure how to handle every possible case. If it wasn't bad enough, the -s and -z can be called multiple times and they would set all values until the main Param value in this case, 35 is called before it prints out the value in base 2.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
            int i,set, zero, number, lenght, count;
    
            for(i = 1; i < argc; i++)
            {
               //checks for the - symbol and determines through the switch what flag was called
               if(argv[i][0] == '-')
               {
                    switch(argv[i][1])
                    {
                            case 'c':
                                    cout << "C flag was called" << "\n";
                                    break;
                            case 's':
                                    cout << "S flag was called" << "\n";
                                    break;
                            case 'z':
                                    cout << "Z flag was called" << "\n";
                                    break;
                            default:
                                    //unknown flag called
                                    cout << "Invalid Flag" << "\n";
                                    break;
                    }
                }
            }
    
            if(argc == 1)
                    cout << "Enter At Least 1 Parameter " << "\n"
            return(0);
    }
    Last edited by cisokay; 05-15-2005 at 02:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21