# Thread: Why does '*' not work? (switch function) [EASY]

1. ## Why does '*' not work? (switch function) [EASY]

Hello everyone! =) I need some help in this case:
Code:
```#include <stdio.h>
#include <stdlib.h>

potenzieren(float basis, int exponent){
int i;
float ergebnis = basis;
for(i=1;i<=exponent;i++)
ergebnis*=basis;
return ergebnis;
}
main (int argc,char *argv[]){
if (argc==2){
if  (strcmp(argv[1],"-help")||strcmp(argv[1],"--help")||strcmp(argv[1],"help")){
/*showtooltip*/
printf("You can use this programm to calculate simple formulas step  by step.\n Operators you can use are:\n'+'\t\t for addition\n'-'\t\t for  subtraction\n'*' \t\t for multiplying\n'/'\t\t for division\n'^'\t\t for  exponential formulas\n'from'\t\t to get the chance of\n\t\t getting [x]  right from [y]amount\n");
}
}

if (argc<4){
printf("Use calculator '[number] [operator] [number]' to get a  result!\n");
exit(1);
}
float z;
if (argc==4){
float x = atof(argv[1]);
float y = atof(argv[3]);
switch (argv[2][0]){
case '+':
z=x+y;
break;
case '-':
z=x-y;
break;
case '/':
z=x/y;
break;
case '*' :        //42 = '*' ASCII
z=x*y;
break;
case '^':
z=potenzieren(x,y);
break;
default : printf("Wrong operator!\n");
break;
}
printf("%s %s %s = %f \n",argv[1], argv[2], argv[3], z);

}
exit(0);
}
```
Everything works right, should only be a simple programm to make easy calculations... But multiplying will not work correctly. =(
I don't know why, I even tried entering 42 as binary code, but won't work...

Some help? =)
Greets

2. I would be more than glad to help but the current formatting is making my eyes stretch to the point of implosion. Please copy and paste this as text within [ code ] brackets and let our site do the formatting.

3. You're using * on the command line, so I think the shell is interpreting that as a wildcard, as shells are wont to do.

4. I would suggest you put at the start of main
Code:
```for ( i = 0 ; i < argc ; i++ ) {
printf("Arg %d = %s\n", i, argv[i] );
}```
What I suspect you will find is that * is one of those wildcard characters processed by your command line shell, so instead of a literal *, you have a nice list of filenames.

One way around this is to say something like
./myprog "2 * 3"
but you'll only have a single argument, and you'll have to parse out the "2" "*" and "3" yourself.

Yeah... you're right it's a wildcard (i'm using ubuntu)
Like mentioned i could put it in a string, but is there a way to ignore those wildcards? a starting preference of the programm or something like that?

and when i add an '-' in front of the operator will it crash too?
ex.: ./calculator 5 -- 2
result = 3?
same for ./calculator 5 -* 2 (=10)
or will it be interpreted as an wildcard again?

6. Originally Posted by Ribbley
Yeah... you're right it's a wildcard (i'm using ubuntu)
Like mentioned i could put it in a string, but is there a way to ignore those wildcards? a starting preference of the programm or something like that?
You could escape it like "\*" so the shell doesn't interpret it but passes it as is to the program.
Originally Posted by Ribbley
and when i add an '-' in front of the operator will it crash too?
ex.: ./calculator 5 -- 2
result = 3?
That simply depends on how well the command line parser is written.
Originally Posted by Ribbley
same for ./calculator 5 -* 2 (=10)
or will it be interpreted as an wildcard again?
It'll be interpreted first by the shell, that's why utilities like bc or dc perform such operations inside their own shell.

7. You could try this
How do I disable pathname expansion in bash? - Ask Ubuntu - Stack Exchange

Or perhaps this
./calc 2 \* 3

Backslash is the universal "escape" character to prevent the next character from being treated as special in some way.

8. You could escape it.

./calculator 5 \* 3

This always works in bash.

9. LOL - everyone's all over this question like a cheap suit

10. okay thanks! seems to work with \*... so i have to add it to the help text - "use '\*' to avoid wildcard usage" or smth like that... thanks !!
I will look here more often for answers ;D