Hi everyone,
When I pass any of the following (-help, help, ?, /?) to this program, I expect it execute the PrintUsage() function, but it doesn't.
I have tested the value of argv[1] before the switch statement, and it showed the correct value, but when it gets to case 2's if/else statement it seems to
return false on the if.
Can anyone tell me what I am doing wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
void PrintUsage(void);
int main(int argc, char* argv[]) {
/* holds the sum */
double dAnswer = 0.0;
/* parse input */
switch(argc) {
/* no input parameters */
case 1:
PrintUsage();
break;
/* only 1 parameter passed; return it */
case 2:
if ((argv[1]=="--help") || (argv[1]=="help") || (argv[1]=="?") || argv[1]=="/?") {
PrintUsage();
}
else {
printf("%.2f\n", atof(argv[1]));
}
break;
/* multiple parameters passed; calculate the sum */
default: {
/* a counter */
int iCount = 0;
/* cycle through input, summing the values */
for (iCount = 1; iCount < argc; iCount++) {
dAnswer += atof(argv[iCount]);
}
/* print sum to the screen */
printf("%.2f\n", dAnswer);
}
}
/* end program */
return 0;
}
void PrintUsage(void) {
/* code removed to conserve space */
}