hoachen
07-14-2005, 05:17 PM
Is anybody know how to simplify the code below? The code work fine just a little messy.
int main(int argc,char *argv[])
{
char c;
int i,j,options[5];
int valid=true;
if(argc<3)
valid = false;
options[0] = false;
options[1] = false;
options[2] = false;
options[3] = false;
options[4] = false;
while((c = getopt (argc, argv, "xxx")) != -1){
if(c=='?'){
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
valid = false;
continue;
}
++i;
switch(c){
case 'a':
options[0] = true;
break;
case 'b':
options[1] = true;
break;
case 'c':
options[2] = true;
break;
case 'd':
options[3] = true;
break;
}
}
if(i==0){
options[0] = true;
options[1] = true;
options[2] = true;
options[3] = true;
}
if(!valid){
fprintf(stderr,"Usage: house [-xxx] FileA FileB\n");
exit(1);
}
}
int main(int argc,char *argv[])
{
char c;
int i,j,options[5];
int valid=true;
if(argc<3)
valid = false;
options[0] = false;
options[1] = false;
options[2] = false;
options[3] = false;
options[4] = false;
while((c = getopt (argc, argv, "xxx")) != -1){
if(c=='?'){
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
valid = false;
continue;
}
++i;
switch(c){
case 'a':
options[0] = true;
break;
case 'b':
options[1] = true;
break;
case 'c':
options[2] = true;
break;
case 'd':
options[3] = true;
break;
}
}
if(i==0){
options[0] = true;
options[1] = true;
options[2] = true;
options[3] = true;
}
if(!valid){
fprintf(stderr,"Usage: house [-xxx] FileA FileB\n");
exit(1);
}
}
