I want myprogram
to take user-entered arguments and see if each argument matches a hard-coded list. I am looking for a better alternative to a long switch
statement or a series of if else
.
Here is a minimal example of what I'm attempting using enum
:
$ ./myprogram blue square
//myprogram.c
#include<stdio.h>
int main(int argc, char ** argv){
//many allowable colors and shapes
enum colors_t {blue, red, /*...*/ green};
enum shape_t {square, circle, /*...*/ triangle};
//how do I check if argv[1] is on the list of colors?
if(COMPARE(argv[1], colors_t)
colors_t user_color = argv[1];
else
printf("%s is not a usable color\n",argv[1]);
//same question with shapes
if(COMPARE(argv[2], shape_t)
shape_t user_shape = argv[2];
else
printf("%s is not a usable shape\n",argv[2]);
return 0;
}
I need help with the COMPARE()
function to see if argv[i]
matches a member of its corresponding enum
list.