Web Hosting Talk







View Full Version : C program


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

JPortal
07-14-2005, 09:43 PM
Well one easy thing to do would be change these blocks:

options[0] = false;
options[1] = false;
options[2] = false;
options[3] = false;
options[4] = false;

To this:

options[0] = options[1] = options[2] = options[3] = options[4] = false;

McJeff215
07-14-2005, 11:41 PM
If you just want to beautify it, run it through 'indent.' The "true" and "false" boolean values aren't part of the C99 specification, they're vendor specific add-ons. They're probably only working because your compiler supports C++ as well (gcc won't eat 'em up by default).

Also.. if valid == false initially, why go through the pain of parsing out all of the arguments to simply return an invallid response?

All in all, it looks like it was written in an HTML textarea, run it through indent!

#define some of those integer values so they make sense... I'm assuming options[3] actually does something, but what?

That "i" defaults thing isn't going to work so hot. What if someone passes one argument, and wants to take default on next?

hoachen
07-15-2005, 08:00 AM
hmmm.. your advice at last part was right, i didn't think about that. Thanks

Originally posted by McJeff215
If you just want to beautify it, run it through 'indent.' The "true" and "false" boolean values aren't part of the C99 specification, they're vendor specific add-ons. They're probably only working because your compiler supports C++ as well (gcc won't eat 'em up by default).

Also.. if valid == false initially, why go through the pain of parsing out all of the arguments to simply return an invallid response?

All in all, it looks like it was written in an HTML textarea, run it through indent!

#define some of those integer values so they make sense... I'm assuming options[3] actually does something, but what?

That "i" defaults thing isn't going to work so hot. What if someone passes one argument, and wants to take default on next?

klyau23
07-16-2005, 12:49 PM
I see there are many repetition in your code. May be use while loop

error404
07-17-2005, 05:36 PM
int options[5] = {0}; is equivalent to setting the elements to zero.