-
Comand line Calculator
Code:
int main (int argc, char* argv[]){
char *ptr = NULL;
double OPE1, OPE2;
char OPERATOR;
double ERG;
if(argc < 4)
{
printf("Error! Nicht genug inputs\n");
return 0;
}
OPE1 = strtod(argv[1], &ptr);
OPE2 = strtod(argv[3], &ptr);
OPERATOR = argv[2][0];
switch(*argv[2])
{ ...... }
- I have been given these instructions : Use strtod() to convert operand1 and operand2 to double. Use strtod() 's parameter endptr to check if the operands were valid.
1) This is my code. Can anyone tell me if i have implemented the instructions correct?
2) I think that switch(argv[2]) is not correct because one of the switch cases is xx and i get as a warning : case label value exceeds maximum value for type
3) I also get the warning : multi-character character constant
I would really appreciate any feedback!
-
"2) I think that switch(argv[2]) is not correct because one of the switch cases is xx and i get as a warning : case label value exceeds maximum value for type"
I don't see any "switch cases".
-
Code:
case'+': ERG=OPE1+OPE2;
printf("Ergebnis = %.1lf \n", ERG);
break;
case '-':
ERG=OPE1-OPE2;
printf("Ergebnis = %.1lf \n", ERG);
break;
case '/':
ERG=OPE1/OPE2;
if(OPE2==0)
{
printf("Error! Division durch Null ist nicht moeglich!\n");
break;
}
else
printf("Ergebnis = %.1lf \n", ERG);
break;
case 'x':
ERG=OPE1 * OPE2;
printf("Ergebnis = %.1lf \n", ERG);
break;
case '_/':
ERG=pow(OPE1, 1/OPE2);
if(OPE2==0)
{
printf("Error!\n");
break;
}
else if(ERG<0)
{
printf("Error!");
break;
}
else
printf("Ergebnis = %.1lf \n", ERG);
case 'xx':
ERG=pow(OPE1, OPE2);
printf("Ergebnis = %.1lf \n", ERG);
these are my switch cases
-
> case '_/':
> case 'xx':
These are out of bounds for things enclosed in single quotes.
It would be useful if you showed us some example command lines, so we can tell you what the best way to handle them would be.
-
This is how the execution should look like :
$ calc.exe 7 + 8
15
$ calc.exe 25 m 100
62.5
$ calc.exe 2 xx 4
16
-
What does m stand for?
A switch only works for integers, so it can work for single characters.
But for strings of characters you need to use if/else if and strcmp.