Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
}

>Solution :

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:"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading