简体   繁体   中英

getopt invalid option command line C

Upon running salary -b -r 4 -t 10 75000 on the command line I am receiving the following errors and am unsure of why. What exactly is the reason I am getting invalid option and what is the solution?

salary: invalid option -- 'r'

salary: invalid option -- 't'

salary: Missing taxes.

usage: salary [-bv] [r rnum] -t tnum base

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int debug = 0;


int main(int argc, char **argv)
{
    extern char *optarg;
    extern int optind;
    int c, err = 0; 
    int tflag=0; 
    double baseSalary,bonus1,bonus2,percentRaise,taxes,finalSalary = 0; 
    
    static char usage[] = "usage: %s [-bv] [r rnum] -t tnum base\n";

    while ((c = getopt(argc, argv, "bc:d:")) != -1)
        switch (c) {
        case 'd':
            debug = 1;
            break;
        case 'b':
            bonus1=5000;
            break;
        case 'v':
            bonus2=6000;
            break;
        case 'r':
            percentRaise = atoi(optarg);
            if ((percentRaise<2) || (percentRaise>10)){
                fprintf(stderr, "%s:Out of bound raise percent.\n", argv[0]);
                exit(1);
            }
            percentRaise/=10;
            percentRaise+=1;
            break;
        case 't':
            taxes = atoi(optarg);
            if ((taxes<5)||(taxes)>30){
                fprintf(stderr, "%s:Out of bound tax percent.\n", argv[0]);
                exit(1);
            }
            taxes /=10;
            taxes = 1-taxes;
            tflag = 1;
            break;
        case '?':
            err = 1;
            break;
        }
    if (tflag == 0) {   /* -c was mandatory */
        fprintf("Result: Missing taxes.\n");
        fprintf(stderr, usage, argv[0]);
        exit(1);
    }  
            
    if (optind < argc){ /* these are the arguments after the command-line options */
        baseSalary = atoi(argv[optind]);
        if ((baseSalary>90000) || (baseSalary<20000)){
            fprintf(stderr, "%s:Out of bound salary\n", argv[0]);
            fprintf(stderr, usage, argv[0]);
            exit(1);
        }
    }
    finalSalary += baseSalary;
    finalSalary+=bonus2;
    finalSalary*=percentRaise;
    finalSalary+=bonus1;
    finalSalary*=taxes;

    printf("Result: %.2f\n", finalSalary);
    exit(0);
}

Your option string doesn't match the options you're expecting.

A letter in the option string followed by a : specifies an option that takes a parameter, while a letter that is not followed by a : is for an option that does not take an argument.

That means your option string "bc:d:" is expecting a b option with no argument, a c option with an argument, and a d option with an argument. That does not correspond to your usage. You instead want:

"bdvr:t:"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM